S
sarathy
Hi all,
I have doubt regarding how objects are passed in C++. The
primary problem of passing by value in C++, is that the destructor of
the object passed will be called twice, thus creating possible damage
to the object passed. If that is the case, why isnt the following
program producing the unexpected o/p. I expect the program to print
In B:rintValues : 22 33
instead of
In B:rintValues : 1 0
Since i have overloaded the copy constructor, i have introduced the
problem mentioned above ( 2 calls to destructor ). But why is the copy
of A in printValues() not printing the actual values in the first call
? Where is the program going wrong?
# include <iostream>
using std::cout;
using std::endl;
class A
{
int x, y;
public:
~A();
A();
A(int , int );
int getX();
int getY();
A(A&);
};
/* Default constructor of A.*/
A::A()
{
cout << "A:: Def constructor" << endl;
}
/* Overloaded constructor of A. */
A::A(int a, int b)
{
x=a;
y=b;
cout << "A:: Prm constructor" << endl;
}
/* Overloaded copy constructor of A. */
A::A(A &a)
{
cout << "In Copy Constructor : " << a.getX() << " " << a.getY() <<
endl;
}
/* Destructor of A. */
A::~A()
{
x=-1;
y=-1;
cout << "A:: Def destructor" << endl;
}
/* Getters. */
int A::getX() { return x; }
int A::getY() { return y; }
class B
{
public:
void printValues(A);
};
void B:rintValues(A a)
{
cout << "In B:rintValues : " << a.getX() << " " << a.getY() << endl;
cout.flush();
}
int main()
{
A a(22,33);
B b;
b.printValues(a);
b.printValues(a);
return 0;
}
OUTPUT
-------------
A:: Prm constructor
In Copy Constructor : 22 33
In B:rintValues : 1 0
A:: Def destructor
In Copy Constructor : 22 33
In B:rintValues : -1 -1
A:: Def destructor
A:: Def destructor
I have doubt regarding how objects are passed in C++. The
primary problem of passing by value in C++, is that the destructor of
the object passed will be called twice, thus creating possible damage
to the object passed. If that is the case, why isnt the following
program producing the unexpected o/p. I expect the program to print
In B:rintValues : 22 33
instead of
In B:rintValues : 1 0
Since i have overloaded the copy constructor, i have introduced the
problem mentioned above ( 2 calls to destructor ). But why is the copy
of A in printValues() not printing the actual values in the first call
? Where is the program going wrong?
# include <iostream>
using std::cout;
using std::endl;
class A
{
int x, y;
public:
~A();
A();
A(int , int );
int getX();
int getY();
A(A&);
};
/* Default constructor of A.*/
A::A()
{
cout << "A:: Def constructor" << endl;
}
/* Overloaded constructor of A. */
A::A(int a, int b)
{
x=a;
y=b;
cout << "A:: Prm constructor" << endl;
}
/* Overloaded copy constructor of A. */
A::A(A &a)
{
cout << "In Copy Constructor : " << a.getX() << " " << a.getY() <<
endl;
}
/* Destructor of A. */
A::~A()
{
x=-1;
y=-1;
cout << "A:: Def destructor" << endl;
}
/* Getters. */
int A::getX() { return x; }
int A::getY() { return y; }
class B
{
public:
void printValues(A);
};
void B:rintValues(A a)
{
cout << "In B:rintValues : " << a.getX() << " " << a.getY() << endl;
cout.flush();
}
int main()
{
A a(22,33);
B b;
b.printValues(a);
b.printValues(a);
return 0;
}
OUTPUT
-------------
A:: Prm constructor
In Copy Constructor : 22 33
In B:rintValues : 1 0
A:: Def destructor
In Copy Constructor : 22 33
In B:rintValues : -1 -1
A:: Def destructor
A:: Def destructor