copy constructor

T

Tony Johansson

Hello!!

Assume you have the following AccountForStudent class

class AccountForStudent
{
public:
AccountForStudent (long, double);
~AccountForStudent ;
long getNumber() const

private:
Student * stud_;
double balance_;
};

and you have a stand alone function equal defined as

bool equal(AccountForStudent as, long number)
{ return as->getNumber() == number; }

// and you instansiate an object kasia of class AccountForStudent in this
way
AccountForStudent kasia(100, 200);

//and you call this function equal in this way
if (equal(kasia,200))
. . . .

Now to my question I just want to check with you that I have understood it
right.

When this function equal is called the copy constructor is used to
initialize object as using kasia.
Now the object as and kasia are sharing the Student object. When the
function equal finish and the object as goes out of scope the destructor for
object as is called and in the destuctor is the object Student deleted.
This will cause the kasia object to have no valid Student object any longer.


//Tony
 
B

BigBrian

Now the object as and kasia are sharing the Student object.

No, nothing is being shared. In the equal() as is a *copy* of kasia,
this copy gets destroyed when it goes out of scope, kasia still exists.

Another way to do this without the copy being created is to declare
equal as follows

bool equal( AccountForStudent const & as, long number )...
 
B

BigBrian

Nevermind my first post, I just noticed that AccountForStudent has a
pointer to a Student object. In the copy, this pointer would point to
the same object as the intial object, thus when it goes out of scope,
IF YOU HAD A destructor which deleted stud_ there would be a problem.
 
K

Karl Heinz Buchegger

Tony said:
Now to my question I just want to check with you that I have understood it
right.

When this function equal is called the copy constructor is used to
initialize object as using kasia.

Yes.
Lets look at it in a more graphical way.

There is this object 'kasia', which upon construction contains
some numbers and a pointer to a valid student object

kasia
+-----------------+
| balance_ : 5.0 | +----------------+
| stud_ : o---------------------->| Student object |
+-----------------+ | |
+----------------+

Now function 'equal()' gets called. 'equal()' takes it first argument
per value, meaning that a copy will be made. Since you didn't specify
a copy constructor of your own, the compiler generated one for you which
does a memberwise copy of all members.

Thus you have

kasia
+-----------------+
| balance_ : 5.0 | +----------------+
| stud_ : o---------------------->| Student object |
+-----------------+ +--------->| |
| +----------------+
|
|
as |
+------------------+ |
| balance_ : 5.0 | |
| stud_ : o------------+
+------------------+

Note how the 'as' object is an exact copy of 'kasia'. All members are duplicated
and contain the exact same values. Of course this includes the pointer too. The
pointer in 'as' contains the same value (points to the same student object) as
the one in 'kasia'.

Now eventually the function finishes and as a result of that the object 'as' gets
destroyed. In the destructor you do a

~AccountForStudent()
{
delete stud_;
}

which gets executed right now: 'as' deletes the student object it has a pointer to.

kasia
+-----------------+
| balance_ : 5.0 |
| stud_ : o---------------------->
+-----------------+ +--------->
|
|
|
as |
+------------------+ |
| balance_ : 5.0 | |
| stud_ : o------------+
+------------------+

.... and the as object itself vanishes:

kasia
+-----------------+
| balance_ : 5.0 |
| stud_ : o---------------------->
+-----------------+

But unfortunately now the 'kasia' object is left with a pointer, that doesn't point
to a valid student object any longer.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top