copy construktor

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
 
V

Victor Bazarov

Tony said:
Assume you have the following AccountForStudent class

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

Forgot parentheses.
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.

Yes, because the first argument of 'equal' is passed by value.
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.

Says who? I don't see AccountForStudent's implementation of either the
constructor or the destructor. For all we know 'stud_' member could be
a shared pointer from some other source and 'AccountForStudent' does not
own it, and so it doesn't create nor does it destroy it.
This will cause the kasia object to have no valid Student object any longer.

It _would_ cause that. However, you cannot expect us to confirm that
without seeing how 'stud_' member is manipulated.

V
 

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,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top