473,854 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using copy constructor

Hello experts!

I have this piece of code. No user defined copy constructor exist.
AccountForStude nt create(long number)
{
AccountForStude nt local(number, 0.0);
return local;
}
int main()
{
AccountForStude nt 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 AccountForStude nt
class AccountForStude nt
{
public:
AccountForStude nt(long number, double balance);
~AccountForStud ent();
long getNumber() const;
. . .
private:
Student* stud_;
double balance_;
};

Many thanks

//Tony
Aug 11 '05 #1
1 2316
Tony Johansson wrote:
Hello experts!

I have this piece of code. No user defined copy constructor exist.
AccountForStude nt create(long number)
{
AccountForStude nt local(number, 0.0);
return local;
}
int main()
{
AccountForStude nt 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
21210
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 pointers) and for objects types call their default constructor. Any others points i should know?
25
2288
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
2989
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 or the default constructor is automatically called instead" Why cant the compiler do this on its own. if we are making an object through copr construction for an inherited class , then why not simply call the corresponding copy constructors for...
4
433
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 objects of the Klocka class in the definition of the Flight class.
14
2725
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
3641
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 copy the same object millions of times. So instead of writing in the copy constructor property1=SourceObject.property1 can't I use memory copy functions to do this faster? Is this too stupid? By the way, I'm a C++ newbie! But don't go easy on me...
11
7419
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 avoid the use of the copy constructor? #include <vector> #include <iostream>
11
1930
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 destruction of the assigned memory seems to call the class destructor more than once. I don't know the reason or whether I used the vector class correctly. Attached is my program. Thanks for your help. Regards,
4
987
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 copy which in itself needs a copy constructor. However i was wondering why can't the existing object be passed as a pointer instead of a reference to the copy constructor which creates a new object.
9
2907
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 Foo() is included, i believe. };
0
9899
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9750
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11024
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10740
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10362
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7909
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4149
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.