member functions returning class type



 DEVELOP > c-Plus-Plus > member functions returning class type

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "eastern_strider"
Date: 26 Dec 2007 07:49:58 PM
Object: 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;
}
.

User: "Kira Yamato"

Title: Re: member functions returning class type 26 Dec 2007 08:28:09 PM
On 2007-12-26 20:49:58 -0500, eastern_strider <oguzakyuz@gmail.com> said:

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
.
User: "Pavel Shved"

Title: Re: member functions returning class type 27 Dec 2007 07:29:04 AM
On 27 Dec, 05:28, Kira Yamato <kira...@earthlink.net> wrote:

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



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:
=A0 A() { cout << "constructor" << endl; }
=A0 A(const A& obj) { cout << "copy constructor" << endl; }
=A0 A foo() { return A(); }
};


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


=A0 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. ;-)
.

User: "eastern_strider"

Title: Re: member functions returning class type 26 Dec 2007 08:34:59 PM
Very good insight; it makes sense to me.
Thanks very much.
On Dec 26, 9:28 pm, Kira Yamato <kira...@earthlink.net> wrote:

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



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

.
User: "Jalen"

Title: Re: member functions returning class type 27 Dec 2007 01:09:02 AM
On Dec 27, 10:34 am, eastern_strider <oguzak...@gmail.com> wrote:

Very good insight; it makes sense to me.

Thanks very much.

On Dec 26, 9:28 pm, Kira Yamato <kira...@earthlink.net> wrote:

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


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
.
User: "Kira Yamato"

Title: Re: member functions returning class type 27 Dec 2007 01:27:40 AM
On 2007-12-27 02:09:02 -0500, Jalen <jiayuewang228@gmail.com> said:

On Dec 27, 10:34 am, eastern_strider <oguzak...@gmail.com> wrote:

Very good insight; it makes sense to me.

Thanks very much.

On Dec 26, 9:28 pm, Kira Yamato <kira...@earthlink.net> wrote:

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


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
.
User: "Jim Langston"

Title: Re: member functions returning class type 28 Dec 2007 10:24:57 AM
Kira Yamato wrote:

On 2007-12-27 02:09:02 -0500, Jalen <jiayuewang228@gmail.com> said:

On Dec 27, 10:34 am, eastern_strider <oguzak...@gmail.com> wrote:

Very good insight; it makes sense to me.

Thanks very much.

On Dec 26, 9:28 pm, Kira Yamato <kira...@earthlink.net> wrote:

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


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
tazmaster@rocketmail.com
.


User: "=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?="

Title: Re: member functions returning class type 27 Dec 2007 06:09:38 AM
On 2007-12-27 08:09, Jalen wrote:

On Dec 27, 10:34 am, eastern_strider <oguzak...@gmail.com> wrote:

Very good insight; it makes sense to me.

Thanks very much.

On Dec 26, 9:28 pm, Kira Yamato <kira...@earthlink.net> wrote:

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


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
.





  Page 1 of 1

1

 


Related Articles
Re: Enable functions in the derived class with the same function name but different return type?
Re: Enable functions in the derived class with the same function name but different return type?
Enable functions in the derived class with the same function name but different return type?
Re: Enable functions in the derived class with the same function name but different return type?
Re: Enable functions in the derived class with the same function name but different return type?
Re: Enable functions in the derived class with the same function name but different return type?
Re: Enable functions in the derived class with the same function name but different return type?
Re: Enable functions in the derived class with the same function name but different return type?
overloaded type cast functions
Why are the min and max values for a particular numeric type implemented as functions?
Functions having same name return different type values in subclasses.
template functions and "return type overloading"
Overloaded functions by its return type, cast rules
Template Type Functions
Dering classes, virtual functions, not sure return type
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER