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

using copy constructor

P: n/a
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
Aug 11 '05 #1
Share this Question
Share on Google+
1 Reply


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

Aug 11 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.