using copy constructor

T

Tony Johansson

Hello experts!

I have this piece of code. No user defined copy constructor exist.
AccountForStudent create(long number)
{
AccountForStudent local(number, 0.0);
return local;
}
int main()
{
AccountForStudent global;
global = create(300);

return 0;
}

Here is what happen according to a book.:
1. Program execution enters the body of create(), and the variable local in
initialized by calling the constructor.
2. A temporary variable, temp, created by the compiler, is initialized by
calling the compiler generated copy constructor because no other is defined
and using the value of the local variable.
3. An assignment operator is called, which assign the value of the temporary
variable temp to global.
4.The destructor for the temporary variable temp is called.
5. create() terminates.

Now to my question: temp and local will share the same Student object
because we have a shallow copy.
When the destructor for temp executes the Student object is deallocated and
therefore you now cannot access the Student object through the variable
global.
The strange thing here is first the destuctor for object local must also
have been called.
When this was called and tries to deallocate the Student object that already
has been deallocated by the temp object. This will as I think destroy the
free heap list and cause a crash. Am I right?.

Here is the class definition for AccountForStudent
class AccountForStudent
{
public:
AccountForStudent(long number, double balance);
~AccountForStudent();
long getNumber() const;
. . .
private:
Student* stud_;
double balance_;
};

Many thanks

//Tony
 
R

Rolf Magnus

Tony said:
Hello experts!

I have this piece of code. No user defined copy constructor exist.
AccountForStudent create(long number)
{
AccountForStudent local(number, 0.0);
return local;
}
int main()
{
AccountForStudent global;
global = create(300);

return 0;
}

Here is what happen according to a book.:

0.5. global is created as a local object in main with the default
constructor.
1. Program execution enters the body of create(), and the variable local
in initialized by calling the constructor.
2. A temporary variable, temp, created by the compiler, is initialized by
calling the compiler generated copy constructor because no other is
defined and using the value of the local variable. 2.5 local is destroyed.
3. An assignment operator is called, which assign the value of the
temporary variable temp to global.
4.The destructor for the temporary variable temp is called.
5. create() terminates.

create() returned before the assingment operator was executed. How else
would the assingment operator be able to copy the value that create()
returned.
Now to my question: temp and local will share the same Student object
because we have a shallow copy.

Do we? You don't show code.
When the destructor for temp executes the Student object is deallocated
and therefore you now cannot access the Student object through the
variable global.

Well, if your assingment operator only makes a shallow copy, yes. If your
default constructor also allocates a Student, then you also have a memory
leak, since global was created with the default constructor, and if the
assignment operator simply overwrites the pointer, you lost the object that
it pointed to before the assignment.
The strange thing here is first the destuctor for object local must also
have been called.
When this was called and tries to deallocate the Student object that
already has been deallocated by the temp object.
This will as I think destroy the free heap list and cause a crash. Am I
right?.

Yes. However, some compilers can optimize away the temporary object.
This is btw. the Rule of Three: If you need one of a copy constructor,
assignment operator or destructor, you can be pretty sure that you need all
three of them.
 

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,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top