473,325 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

using copy constructor

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
1 2208
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
25
by: rokia | last post by:
in a project, I use many,many stl such as stack,list,vctor etc. somewhere the vector's size is more than 2K. is this a efficient way?
8
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor...
4
by: Tony Johansson | last post by:
Hello Experts! I have this constructor for class Flight. Flight::Flight(string flight_no, Klocka dep_t, Klocka arr_t) : no(flight_no), dep(dep_t), arr(arr_t) {} Both dep and arr are...
14
by: Arne | last post by:
In C++ we have a copy constructor. What is the equivalent in .Net? Would that be a clone method?
24
by: rdc02271 | last post by:
Hello! Is this too crazy or not? Copy constructor: why can't I copy objects as if they were structs? I have a set of simple objects (no string properties, just integers, doubles) and I have to...
11
by: PengYu.UT | last post by:
The following program calls the normal constructor and the copy constructor. By calling the copy constuctor is redundandant, all I want is only a vector of a trial object. Is there any way to...
11
by: Brian | last post by:
Dear Programmers, I have a class with a pointer to an array. In the destructor, I just freed this pointer. A problem happens if I define a reference to a vector of this kind of class. The...
4
by: Rahul | last post by:
Hi Everyone, It is well known that the input parameter which is passed to the copy constructor is passed as reference and not as as object. Because passing an object is as good as making another...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.