468,497 Members | 1,877 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,497 developers. It's quick & easy.

member functions returning class type

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;
}

Dec 27 '07 #1
7 1030
On 2007-12-26 20:49:58 -0500, eastern_strider <og*******@gmail.comsaid:
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.

--

-kira

Dec 27 '07 #2
Very good insight; it makes sense to me.

Thanks very much.

On Dec 26, 9:28 pm, Kira Yamato <kira...@earthlink.netwrote:
On 2007-12-26 20:49:58 -0500, eastern_strider <oguzak...@gmail.comsaid:
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.

--

-kira
Dec 27 '07 #3
On Dec 27, 10:34 am, eastern_strider <oguzak...@gmail.comwrote:
Very good insight; it makes sense to me.

Thanks very much.

On Dec 26, 9:28 pm, Kira Yamato <kira...@earthlink.netwrote:
On 2007-12-26 20:49:58 -0500, eastern_strider <oguzak...@gmail.comsaid:
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.
--
-kira
This problem can't be reproduced on Microsoft Visual C++ 6.0.
Line c will invoke the copy constructor.
--
-Jalen
Dec 27 '07 #4
On 2007-12-27 02:09:02 -0500, Jalen <ji***********@gmail.comsaid:
On Dec 27, 10:34 am, eastern_strider <oguzak...@gmail.comwrote:
>Very good insight; it makes sense to me.

Thanks very much.

On Dec 26, 9:28 pm, Kira Yamato <kira...@earthlink.netwrote:
>>On 2007-12-26 20:49:58 -0500, eastern_strider <oguzak...@gmail.comsaid:
>>>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.
>>--
>>-kira

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.

--

-kira

Dec 27 '07 #5
On 2007-12-27 08:09, Jalen wrote:
On Dec 27, 10:34 am, eastern_strider <oguzak...@gmail.comwrote:
>Very good insight; it makes sense to me.

Thanks very much.

On Dec 26, 9:28 pm, Kira Yamato <kira...@earthlink.netwrote:
On 2007-12-26 20:49:58 -0500, eastern_strider <oguzak...@gmail.comsaid:
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.

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.

--
Erik Wikström
Dec 27 '07 #6
On 27 Dec, 05:28, Kira Yamato <kira...@earthlink.netwrote:
On 2007-12-26 20:49:58 -0500, eastern_strider <oguzak...@gmail.comsaid:
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.
Mhm, this optimization is described in c++-faq-lite. ;-)
Dec 27 '07 #7
Kira Yamato wrote:
On 2007-12-27 02:09:02 -0500, Jalen <ji***********@gmail.comsaid:
>On Dec 27, 10:34 am, eastern_strider <oguzak...@gmail.comwrote:
>>Very good insight; it makes sense to me.

Thanks very much.

On Dec 26, 9:28 pm, Kira Yamato <kira...@earthlink.netwrote:

On 2007-12-26 20:49:58 -0500, eastern_strider
<oguzak...@gmail.comsaid:

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.

--

-kira

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.
MSVC++ .net 2003 reproduces the OPs output. VSVC++ 6.0 sucks, it was
pre-standard.

--
Jim Langston
ta*******@rocketmail.com
Dec 28 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Anthony Jones | last post: by
39 posts views Thread by JKop | last post: by
2 posts views Thread by Per | last post: by
21 posts views Thread by jimmy | last post: by
5 posts views Thread by jmd | last post: by
4 posts views Thread by tech | last post: by
7 posts views Thread by Immortal Nephi | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.