By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

More instantiation and ctor call

P: n/a
class C
{
public:
C() { cout << "ctor" << endl; }
C(const C & rhs) { cout << "copy ctor" << endl; }
~C() { cout << "dtor" << endl; }
};

C c0; // A
C c1 = C(); // B

I could imagine, that line B causes the creation of a temporary object which
is used to intantiate c1 by means of the copy ctor. But it's not the case,
lines A and B cause the same (tested on several compilers). Is this
required? Or is the compiler free which way to do that? Is it optimization?

Thanks!
Robert Sturzenegger
Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"Robert Sturzenegger" <ro*****************@abc.ch> wrote in message
news:3f******@news.swissonline.ch...
class C
{
public:
C() { cout << "ctor" << endl; }
C(const C & rhs) { cout << "copy ctor" << endl; }
~C() { cout << "dtor" << endl; }
};

C c0; // A
C c1 = C(); // B

I could imagine, that line B causes the creation of a temporary object which is used to intantiate c1 by means of the copy ctor. But it's not the case,
lines A and B cause the same (tested on several compilers). Is this
required? Or is the compiler free which way to do that? Is it optimization?
Thanks!
Robert Sturzenegger


When you write A a = x; the effect is the same as A a(x); provided x is an
expression of the same type as A. Otherwise it is a construction of a
temporary object. This is a requirement, not an optional optimization.

john
Jul 19 '05 #2

P: n/a

"John Harrison" <jo*************@hotmail.com> wrote in message
news:bg************@ID-196037.news.uni-berlin.de...

"Robert Sturzenegger" <ro*****************@abc.ch> wrote in message
news:3f******@news.swissonline.ch...
class C
{
public:
C() { cout << "ctor" << endl; }
C(const C & rhs) { cout << "copy ctor" << endl; }
~C() { cout << "dtor" << endl; }
};

C c0; // A
C c1 = C(); // B

I could imagine, that line B causes the creation of a temporary object

which
is used to intantiate c1 by means of the copy ctor. But it's not the case, lines A and B cause the same (tested on several compilers). Is this
required? Or is the compiler free which way to do that? Is it

optimization?

Thanks!
Robert Sturzenegger


When you write A a = x; the effect is the same as A a(x); provided x is an
expression of the same type as A. Otherwise it is a construction of a
temporary object. This is a requirement, not an optional optimization.

john


What I said is true, but I'm not sure it answers your questions, as it only
means

C c1 = C();

is equivalent to

C c1(C());

Not sure what happens from that point on, sorry.

john
Jul 19 '05 #3

P: n/a

"Robert Sturzenegger" <ro*****************@abc.ch> wrote in message
news:3f******@news.swissonline.ch...
class C
{
public:
C() { cout << "ctor" << endl; }
C(const C & rhs) { cout << "copy ctor" << endl; }
~C() { cout << "dtor" << endl; }
};

C c0; // A
C c1 = C(); // B

I could imagine, that line B causes the creation of a temporary object which is used to intantiate c1 by means of the copy ctor. But it's not the case,
lines A and B cause the same (tested on several compilers). Is this
required? Or is the compiler free which way to do that? Is it optimization?

Seems like compiler optimisation to me.
IMO, C c1(C()) may bypass the copy constructor and directly make the
temporary object c1.

--
With best wishes,
J.Schafer
Jul 19 '05 #4

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote in message news:<bg************@ID-196037.news.uni-berlin.de>...
"John Harrison" <jo*************@hotmail.com> wrote in message
news:bg************@ID-196037.news.uni-berlin.de...

"Robert Sturzenegger" <ro*****************@abc.ch> wrote in message
news:3f******@news.swissonline.ch...
class C
{
public:
C() { cout << "ctor" << endl; }
C(const C & rhs) { cout << "copy ctor" << endl; }
~C() { cout << "dtor" << endl; }
};

C c0; // A
C c1 = C(); // B

I could imagine, that line B causes the creation of a temporary object which is used to intantiate c1 by means of the copy ctor. But it's not the case, lines A and B cause the same (tested on several compilers). Is this
required? Or is the compiler free which way to do that? Is it optimization?
Thanks!
Robert Sturzenegger


When you write A a = x; the effect is the same as A a(x); provided x is an
expression of the same type as A. Otherwise it is a construction of a
temporary object. This is a requirement, not an optional optimization.

john


What I said is true, but I'm not sure it answers your questions, as it only
means

C c1 = C();

is equivalent to

C c1(C());

Not sure what happens from that point on, sorry.

john


I believe the compiler is allowed to optimise away the construction of
the temporary C and default construct c1 directly.

GJD
Jul 19 '05 #5

P: n/a


Robert Sturzenegger wrote:

class C
{
public:
C() { cout << "ctor" << endl; }
C(const C & rhs) { cout << "copy ctor" << endl; }
~C() { cout << "dtor" << endl; }
};

C c0; // A
C c1 = C(); // B

I could imagine, that line B causes the creation of a temporary object which
is used to intantiate c1 by means of the copy ctor. But it's not the case,
lines A and B cause the same (tested on several compilers). Is this
required? Or is the compiler free which way to do that? Is it optimization?


Your imagination is right: the compiler has to do this and it also has to check,
if this path is going to work (by eg. checking the accessability of the copy constructor).
But: The compiler is also allowed to optimize the call of the copy constructor away.
This is explicitely allowed by the C++ standard, even in the case that the copy
constructor has side effects as in your case.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #6

P: n/a
Thank you for your responses!
Robert
Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.