473,569 Members | 3,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

copy constructor and return by value

Hello all,

Even though this topic has been discussed many times, I still need your
help in clearing my confusions. Consider the following code:

class aclass{

public:

aclass(){
cout<<"calling constructor for: "<<this<<en dl;
}
aclass(const aclass &b){
cout<<"calling copy constructor for: "<<this<<" copying from:
"<<&b<<endl ;
}
void print(){
cout<<"why me?"<<endl;
}
~aclass(){
cout<<"destruct or for: "<<this<<en dl;
}

};

aclass myf(){
aclass a;
return a;
}

aclass myf2(aclass a){
return a;
}

I have noticed that when I called myf(), from main, no copy constructor
is called. Which I guess is because of the optimization by the
compiler. That is if I run the code as:

myf();
aclass t1 = myf();
aclass t2(myf());

No copy constructor is called. Is there any situation in which copy
constructor will be called for myf() function? what would be the case?

I have also noticed that when I called myf2(..), from main, copy
constructor is called twice (one for the argument passed by value and
one for the return by value) and I am guessing no optimization is done
there. What is the deal here then? What goes in the compiler?

Local object are destructed when the object goes out of the scope. But
in the following situation:

int main(){
myf().print();
}

the destructor is called after the print() function of aclass. What is
the deal?

Thanks for your help...thank for taking time...

Bipod

Jul 29 '05 #1
3 2418
Hi Bipod,
First of all there is no compiler optimization concept in this context.
When you are creating a copy of existing object the copy constructor is
called... And when you are constructing the object in isolation the
normal constructor will be called... I'll try to explain a bit in deep
with the examples that you have mentioned in your post.
1. myf()
inside myf() a object is created from the dust... So normal
constructor gets called.
2. aclass t1 = myf()
In this first a object is created in myf() function which is because
of default constructor. And you are creating a copy of object that is
returned by myf() function. So copy constructor is called which creates
object t1.
3. aclass t2(myf())
t2 is the copy of the object which is returned by myf(). here default
constructor is called when control is inside myf() and copy constructor
is called while creating t2 because t2 is a copy of t1.
4. myf().print()
if you are evaluating an expression some temporary objects gets
created. These gets destructed only after the expression evaluation...
So in this example the destructor is called after print() function is
evaluated.

Please correct me if I am wrong anywhere.

Jul 29 '05 #2
bi***********@g mail.com wrote:
Even though this topic has been discussed many times, I still need your
help in clearing my confusions. Consider the following code:

class aclass{

public:

aclass(){
cout<<"calling constructor for: "<<this<<en dl;
}
aclass(const aclass &b){
cout<<"calling copy constructor for: "<<this<<" copying from:
"<<&b<<endl ;
}
void print(){
cout<<"why me?"<<endl;
}
~aclass(){
cout<<"destruct or for: "<<this<<en dl;
}

};

aclass myf(){
aclass a;
return a;
}

aclass myf2(aclass a){
return a;
}

I have noticed that when I called myf(), from main, no copy constructor
is called. Which I guess is because of the optimization by the
compiler. That is if I run the code as:

myf();
OK. A function call. The compiler can optimize away the creation of
the return value since it's not used.
aclass t1 = myf();
OK. Copy-initialisation. Actual copying can be optimized away by the
compiler, and 't1' can serve as the destination for the return value.
aclass t2(myf());
This is a declaration of a function. No object is constructed.
No copy constructor is called. Is there any situation in which copy
constructor will be called for myf() function? what would be the case?

I have also noticed that when I called myf2(..), from main, copy
constructor is called twice (one for the argument passed by value and
one for the return by value) and I am guessing no optimization is done
there. What is the deal here then? What goes in the compiler?
Why don't you ask in a newsgroup dedicated to your compiler? Or make your
compiler generate the assembly code for you and take a closer look at what
the compiler creates...
Local object are destructed when the object goes out of the scope. But
in the following situation:

int main(){
myf().print();
}

the destructor is called after the print() function of aclass. What is
the deal?


The temporary object lives as long as needed, and is destroyed as the last
step in evaluating the _full_expressio n_ in which it was constructed.

V
Jul 29 '05 #3
Victor Bazarov wrote:
aclass t2(myf());

This is a declaration of a function. No object is constructed.


My bad. It's not a declaration of a function.
[...]

Jul 29 '05 #4

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

Similar topics

42
5728
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC...
5
459
by: I wish | last post by:
I wrote some program #include <iostream> class A { public: A() {} A( const A& t ) { std::cout << "Good" << std::endl; } }; A f()
1
2214
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 pertaining to casting class A to class B, the implementation of the
3
2091
by: Tony Johansson | last post by:
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() {
10
2538
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, we do not need to use the copy-constructor. So depending on the above reasoning I can avoid call by value and return by value for class objects,...
1
2113
by: blangela | last post by:
3.0 Advanced Topic Addendum There are a few cases where the C++ compiler cannot provide an overloaded assignment operator for your class. If your class contains a const member or/and a reference member, the compiler will not be able to synthesize an assignment operator for your class. It actually helps to think of a reference member as a...
13
2448
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 certain code syntax but more a 'code architecture' thing , I'll use simple example classes (which are certainly not complete or working...) just to...
10
2947
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 private. But the Digital Mars compiler does not complain. Which compiler is right? (And if gcc is right, why does this restriction exist?) Szabolcs
22
3598
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float v);
0
7703
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...
1
7681
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...
0
6290
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
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...
0
5228
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...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2118
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
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
950
bsmnconsultancy
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...

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.