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 1 2297
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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?
|
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?
|
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...
|
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.
|
by: Arne |
last post by:
In C++ we have a copy constructor. What is the equivalent in .Net? Would that
be a clone method?
| |
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...
|
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>
|
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,
|
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.
|
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.
};
|
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,...
| |
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...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
|
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...
|
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...
|
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();...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |