Copy Constructor and Initialization by Temporaries

A

anujanujdhamija

I am initializing a class variable using a temporary, example:
abc a1, a2;
abc a3 = a1+a2; (See prog below)

I expect a copy constructor to be invoked for initialization of a3. So
in all, I expect 4 constructors and 4 destructors to be invoked for
following program-> one each for a1, a2 and a3 and fourth for temporary
variable in operator+() method.

But to my utter surprise I see only 3 constructors and 3 destructors
getting invoked, one each for a1, a2 and temporary variable. No
constructor or destructor are invoked for a3. (Output at end)

Is it following some different rule for initialization using
temporaries or is it due to some optimization by compiler? If its not
optimization then isn't a3 treaed like a reference to temporary in this
case?

I am compiling it using g++ compiler on cygwin/windows..

/*********************************************************************************/
//Prog.cpp

#include<iostream>
using namespace std;

class abc
{
public:
int p;
string name;
abc(string n):name(n)
{
cout<<"Default"<<endl;
}
abc (const abc &a):name(a.name + "__")
{
cout<<"Copy"<<endl;
}

~abc()
{
cout<<"Destroying:"<<name<<endl;
}

abc& operator =(abc a1)
{
cout<<"Assignment"<<endl;
return *this;
}
};

abc operator +(abc& a1,abc& a2)
{
cout<<"+"<<endl;
abc temp("temp");
temp.p = a1.p + a2.p;
return temp;
}

int main()
{
abc a1("a1");
abc a2("a2");;
abc a3=a1+a2;

return 0;
}
/*********************************************************************************/
/////////////Output:
Default
Default
+
Default
Destroying:temp
Destroying:a2
Destroying:a1


regards
AD
 
J

Jonathan Mcdougall

I am initializing a class variable using a temporary, example:
abc a1, a2;
abc a3 = a1+a2; (See prog below)

I expect a copy constructor to be invoked for initialization of a3. So
in all, I expect 4 constructors and 4 destructors to be invoked for
following program-> one each for a1, a2 and a3 and fourth for temporary
variable in operator+() method.

But to my utter surprise I see only 3 constructors and 3 destructors
getting invoked, one each for a1, a2 and temporary variable. No
constructor or destructor are invoked for a3. (Output at end)

Is it following some different rule for initialization using
temporaries or is it due to some optimization by compiler? If its not
optimization then isn't a3 treaed like a reference to temporary in this
case?

I am compiling it using g++ compiler on cygwin/windows..

/*********************************************************************************/
//Prog.cpp

#include<iostream>
using namespace std;

class abc
{
public:
int p;
string name;
abc(string n):name(n)
{
cout<<"Default"<<endl;
}
abc (const abc &a):name(a.name + "__")
{
cout<<"Copy"<<endl;
}

~abc()
{
cout<<"Destroying:"<<name<<endl;
}

abc& operator =(abc a1)
{
cout<<"Assignment"<<endl;
return *this;
}
};

abc operator +(abc& a1,abc& a2)
{
cout<<"+"<<endl;
abc temp("temp");
temp.p = a1.p + a2.p;
return temp;
}

int main()
{
abc a1("a1");
abc a2("a2");;
abc a3=a1+a2;

return 0;
}
/*********************************************************************************/
/////////////Output:
Default
Default
+
Default
Destroying:temp
Destroying:a2
Destroying:a1

This is most likely an optimization where the compiler was able to
construct a3 directly, without using a temporary. You could imagine a3
being passed by address as a third parameter to operator+ to be
initialized, replacing "temp".


Jonathan
 
A

anujanujdhamija

I thought optimization is turned off by default but seems that is not
the case.

Can you suggest me how to turn optimization off so that I can see the
standard behavior.
 
B

BobR

(e-mail address removed) wrote in message
That's a question specific to your compiler so you'll need to ask a
group specific to your compiler. The FAQ for this group has suggestions
for where to post g++ questions.

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9
Gavin Deane

<OT> FYI
For g++, it's off by default. You turn it on with -O, -O2, -O3 or -Os. If
something else turned it on, you can turn it off with -O0 (that's capital oh
zero).
</OT>
 
J

Jonathan Mcdougall

(e-mail address removed) wrote:

Please quote the messaeg you are answering to.
I thought optimization is turned off by default but seems that is not
the case.

Depends on the compiler.

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9
Can you suggest me how to turn optimization off so that I can see the
standard behavior.

This is a standard behavior, the compiler "is allowed to omit the copy
construction of a class object, even if the copy constructor and/or
destructor for the object have side effects."


Jonathan
 
X

Xiaoshen Li

I ran your program and got the followint output:

Default
Default
+
Default
Copy
Destroying:temp
Destroying:temp__
Destroying:a2
Destroying:a1

So, there are four destructors. I just used "g++ prog.cpp" and
"./a.out". I am compiling it on linux.

BTW, it seems you missed #include <string> in your program. I am not
sure, maybe I am wrong.

I don't understand why there is no "Assigment" printed out.
 
S

Shezan Baig

Xiaoshen said:
I don't understand why there is no "Assigment" printed out.


That is because there is no assignment being called:



This code *constructs* an object 'a3' using the copy constructor (which
can be optimized away). Even though the '=' symbol is used in the
code, its really a construction and not an assignment.


Hope this helps,
-shez-
 
M

Manish

To invoke assignment operator, you will have to do the following:

int main()
{
abc a1("a1");
abc a2("a2");;
abc a3("0");

a3=a1+a2;

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top