473,498 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Copy Constructor and Initialization by Temporaries

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

Dec 28 '05 #1
8 2074

an*************@gmail.com wrote:
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

Dec 28 '05 #2
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.

Dec 28 '05 #3

an*************@gmail.com wrote:
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


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-lit...t.html#faq-5.9

Gavin Deane

Dec 28 '05 #4

de*********@hotmail.com wrote in message
<11*********************@z14g2000cwz.googlegroups. com>...

an*************@gmail.com wrote:
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


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-lit...t.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>

--
Bob R
POVrookie
Dec 28 '05 #5
an*************@gmail.com 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-lit...t.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

Dec 28 '05 #6
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.

an*************@gmail.com wrote:
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


Jan 3 '06 #7
Xiaoshen Li wrote:
I don't understand why there is no "Assigment" printed out.

That is because there is no assignment being called:

abc a3 = a1+a2; (See prog below)

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-

Jan 4 '06 #8
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;
}

Jan 4 '06 #9

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

Similar topics

6
2975
by: Alexander Stippler | last post by:
Hi, I wonder about the behaviour of como and icc on some very simple program. I thought initializing members of classes, which are of class type, would be 'direct initialized' (as the standard...
6
7496
by: Christoph Bartoschek | last post by:
Hi, gcc 3.4 rejects the following program: class T { public: T() : a(3) {} explicit T(T const & other) : a(other.a) {} private: int a;
11
580
by: Kurt Krueckeberg | last post by:
Given a class X class X { public: X(int); X(const X&); //. . . }; Is this line X x1(1); the same thing as X x2 = 2;
13
4982
by: blangela | last post by:
I have decided (see earlier post) to paste my Word doc here so that it will be simpler for people to provide feedback (by directly inserting their comments in the post). I will post it in 3 parts...
32
2192
by: seank76 | last post by:
While going through my company's existing codebase, I saw a bunch of weird lines. Take a look at this one: class A { public: A(Foo *f) : _f(f) {}
22
3586
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...
3
361
by: subramanian100in | last post by:
I thought the copy ctor always takes the form Test::Test(const Test & arg); for a class Test. But I read the following sentence in Stanley Lippman's C++ Primer 4th Edition(Page 476): The...
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...
10
1865
by: abhash | last post by:
I am bit puzzled at the following piece of code I tried: ---------------------------------------------------------------------------------- #include <iostream> using namespace std; class Test...
0
7002
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
7165
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,...
1
6887
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
7379
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
5462
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,...
1
4910
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...
0
4590
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...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1419
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 ...

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.