Copy Constructor Qtn

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::printValues : 22 33

instead of

In B::printValues : 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::printValues(A a)
{
cout << "In B::printValues : " << 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::printValues : 1 0
A:: Def destructor
In Copy Constructor : 22 33
In B::printValues : -1 -1
A:: Def destructor
A:: Def destructor
 
B

Bo Persson

sarathy said:
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.

Why? If you make a copy, you will later have to destruct both the
original and the copy.
If that is the case, why isnt the following
program producing the unexpected o/p. I expect the program to print

In B::printValues : 22 33

instead of

In B::printValues : 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?

You have a copy constructor that prints values, but it doesn't copy
them.
/* Overloaded copy constructor of A. */
A::A(A &a)
{
cout << "In Copy Constructor : " << a.getX() << " " << a.getY() <<
endl;
}
OUTPUT
-------------

A:: Prm constructor
In Copy Constructor : 22 33
In B::printValues : 1 0
A:: Def destructor
In Copy Constructor : 22 33
In B::printValues : -1 -1
A:: Def destructor
A:: Def destructor

I see one construction of an A, and two copies. That matches the three
destructors.


Bo Persson
 
T

Thomas J. Gritzan

sarathy said:
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.

It is not the same object that is destructed twice. The original is
desctructed once, and every copy is destructed once.
If that is the case, why isnt the following
program producing the unexpected o/p. I expect the program to print

In B::printValues : 22 33

instead of

In B::printValues : 1 0
[...]

See below.
# include <iostream>

using std::cout;
using std::endl;

class A
{
int x, y;

public:
~A();
A();
A(int , int );
int getX();
int getY();

int getX() const;
int getY() const;
A(A&);
}; [...]
/* Overloaded copy constructor of A. */
A::A(A &a)

Should better be:

A::A(const A& a)
{
cout << "In Copy Constructor : " << a.getX() << " " << a.getY() <<
endl;
}

Your copy constructor doesn't copy construct your object. You have to
initialize x and y of the constructed object with some values.
 
R

Rolf Magnus

sarathy said:
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.

How did you come to that conclusion? It's not true.
If that is the case, why isnt the following program producing the
unexpected o/p. I expect the program to print

In B::printValues : 22 33

instead of

In B::printValues : 1 0

If you wanted the values copied over to the new object, you'd actually have
to copy them in your copy constructor.
Since i have overloaded the copy constructor, i have introduced the
problem mentioned above ( 2 calls to destructor ).

No, you haven't. When you pass an object by value, it gets copied. Storage
is acquired for that copy, then your constructor is called to initialize
it. Your constructor just happens to keep it uninitialized.
But why is the copy of A in printValues() not printing the actual values
in the first call ?

The actual values are undefined.
Where is the program going wrong?

Where your copy constructor happens to keep the "copy" uninitialized.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,573
Members
45,046
Latest member
Gavizuho

Latest Threads

Top