By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,954 Members | 1,133 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,954 IT Pros & Developers. It's quick & easy.

Copy Constructor Qtn

P: n/a
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

Aug 18 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
sarathy schrieb:
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.

--
Thomas
Aug 18 '06 #2

P: n/a

"sarathy" <sp*********@gmail.comskrev i meddelandet
news:11**********************@h48g2000cwc.googlegr oups.com...
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
Aug 18 '06 #3

P: n/a
sarathy wrote:
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.

Aug 18 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.