member functions returning class type

E

eastern_strider

Hi,

In the following code snippet, if I comment out line b, the output
becomes:

constructor
constructor

whereas if I comment out line c, the output becomes:

constructor
copy constructor

Any explanations regarding what is causing this difference is
appreciated.


#include <iostream>
using namespace std;

class A {
public:
A() { cout << "constructor" << endl; }
A(const A& obj) { cout << "copy constructor" << endl; }
A foo() { return A(); }
};

int main()
{
A a;
A b(a); // <--- line b
A c(a.foo()); // <--- line c

return 0;
}
 
K

Kira Yamato

Hi,

In the following code snippet, if I comment out line b, the output
becomes:

constructor
constructor

whereas if I comment out line c, the output becomes:

constructor
copy constructor

Any explanations regarding what is causing this difference is
appreciated.


#include <iostream>
using namespace std;

class A {
public:
A() { cout << "constructor" << endl; }
A(const A& obj) { cout << "copy constructor" << endl; }
A foo() { return A(); }
};

int main()
{
A a;
A b(a); // <--- line b
A c(a.foo()); // <--- line c

return 0;
}

Interesting find on this C++ behavior.

I'm guessing it's a compiler optimization.

In line b, you're declaring another object that is different from a.
So, the compiler had to invoke the copy constructor to create another
object (b).

However, in line c, the call a.foo() returns a temporary object, call
it t. This object t is already another object that is different from
a. So, the compiler most likely just aliased the variable c to t, to
avoid an unnecessary object copying.

The scope of t is now the body of main and not line c anymore. That
is, it won't be cleaned up at the end of line c, but instead, it will
be cleaned up at the end of main.
 
J

Jalen

Very good insight; it makes sense to me.

Thanks very much.

This problem can't be reproduced on Microsoft Visual C++ 6.0.
Line c will invoke the copy constructor.
 
K

Kira Yamato

This problem can't be reproduced on Microsoft Visual C++ 6.0.
Line c will invoke the copy constructor.

It is certainly reproducbile in g++ 4.0.1.

I guess that just say MSVC++ sucks. :)

Or maybe there is some check buttons you need to click before enabling
certain optimizations that are apparently not on by default.
 
E

Erik Wikström

This problem can't be reproduced on Microsoft Visual C++ 6.0.
Line c will invoke the copy constructor.

I might be a bit negative towards VC++ 6, but frankly I do not expect
anything to be reproducible on VC++ 6. It is to old and it does not
support a number of things in the C++ standard.
 
J

Jim Langston

Kira said:
It is certainly reproducbile in g++ 4.0.1.

I guess that just say MSVC++ sucks. :)

Or maybe there is some check buttons you need to click before enabling
certain optimizations that are apparently not on by default.

MSVC++ .net 2003 reproduces the OPs output. VSVC++ 6.0 sucks, it was
pre-standard.
 

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,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top