473,396 Members | 1,892 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,396 software developers and data experts.

more copy constructor when returning value

Hello!!

Assume you have the following AccountForStudent class

class AccountForStudent
{
public:
AccountForStudent ();
AccountForStudent (long, double);
~AccountForStudent ;
long getNumber() const

private:
Student * stud_;
double balance_;
};

and you have a stand alone function create defined as

AccountForStudent create(long number )
{
AccountForStudent local(number, 0.0);
return local;
}

// and you instansiate an object called global of class AccountForStudent in
this way
AccountForStudent global;

//and you call this stand alone function create in this way
global = create(300);
. . . .

Now to my question I just want to check with you that I have understood it
right.

When this function create is called a local object named local is created in
the function.
When the function create is doing return local the copy constructor is
called and
a temporary object is created lets call it temp. Now the object temp and
local is sharing the Student object.

We can imagine the code behind is something like
AccountForStudent temp(local);
this mean an object temp is created and initialized using object local.
This object temp is assigned to object global using the assignment operator.
After this statement global = create(300);
the temp object destructor is called and here we do delete Student leaving
the global object without any Student object.

Give me some feedback on this please.
Correct me if I'm wrong in some place.
//Tony


Jul 23 '05 #1
1 1086
Tony Johansson wrote:

Give me some feedback on this please.
Correct me if I'm wrong in some place.


Listen.
It doesn't matter when or where that copy constructor is used.
At the moment it is used you have that problem, be it when
passing an object by value or returning an object per value.

At the moment the copy constructor makes what we call a 'shallow copy'
(a memberwise copy) you run into troubles with that type of setup.

It is the fact that the copy constructor does the wrong thing, that
creates that problem. When or where the copy constructor is used
is unimportant.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): ...
4
by: Terry | last post by:
Hi, Can someone please explain what a copy constructor does and how it's useful? Thanks, Terry
1
by: masood.iqbal | last post by:
I have a few questions regarding overloaded typecast operators and copy constructors that I would like an answer for. Thanks in advance. Masood (1) In some examples that I have seen...
2
by: utab | last post by:
Dear all, I have got a simple example to demonstrate the need for copy construction but one point is not clear to me. When returning from the function in main h2 is not constructed why? I marked...
10
by: utab | last post by:
Dear all, So passing and returning a class object is the time when to include the definition of the copy constructor into the class definition. But if we don't call by value or return by value, ...
2
by: flamexx7 | last post by:
http://www.rafb.net/paste/results/V3TZeb28.html In this code, why copy constructor is not called while returning object from no_arg() . I was trying to find answer in C++ Standard. and there it's...
5
by: sarathy | last post by:
Hi, I need to see the output of the program when no copy constructor is used. Since by default, there is an implicit one, how should i perform the override, so as to totally remove the copy...
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
10
by: SzH | last post by:
The code below demonstrates that the copy constructor of moo is not called on the first line of main. In spite of this, g++ (version 4.1.2) refuses to compile it if I make the copy constructor...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.