Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Copy Constructor Qtn

Reply
Thread Tools

Copy Constructor Qtn

 
 
sarathy
Guest
Posts: n/a
 
      08-18-2006
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

 
Reply With Quote
 
 
 
 
Bo Persson
Guest
Posts: n/a
 
      08-18-2006

"sarathy" <(E-Mail Removed)> skrev i meddelandet
news:(E-Mail Removed) 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: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?


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:rintValues : 1 0
> A:: Def destructor
> In Copy Constructor : 22 33
> In B:rintValues : -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


 
Reply With Quote
 
 
 
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      08-18-2006
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:rintValues : 22 33
>
> instead of
>
> In B:rintValues : 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
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      08-18-2006
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:rintValues : 22 33
>
> instead of
>
> In B:rintValues : 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
template copy constructor vs normal copy constructor cinsk C++ 35 10-10-2010 11:14 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Calling base class constructor from derived class Copy constructor ali C++ 4 03-05-2007 09:15 AM
deep/shallow copy - constructor v Object.copy() VisionSet Java 8 04-29-2004 10:41 PM
Copy constructor hides default constructor Aire C++ 3 01-25-2004 05:47 PM



Advertisments