472,784 Members | 1,034 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,784 software developers and data experts.

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 1137
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Me | last post by:
Hi, I would like learn from people with experience in C++, which of the following styles of way to construct "get/set" member functions would be the best in terms of usability, speed, et cetera. ...
12
by: Anthony Jones | last post by:
Just a bit of background: I'm one of a group of FORTRAN programmers, looking to switch to C++. We are trying to write a few simple examples to demonstrate the power of the language to our manager,...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
39
by: JKop | last post by:
Back when I read my first C++ book, I was given the following scenario: class Cheese { public: int number_of_holes; int colour;
2
by: Per | last post by:
Hi! Does it take longer time to initialize a class with many member-functions than one with fewer? Will the sizeof (Some Class) return more bytes on a class with more member-functions etc... /p
21
by: jimmy | last post by:
Hi, I would like to initialize a member array of 3 floats with a memcpy (for reasons of efficiency). Later I would like to access the floats via public members (e.g. foo.x, foo.y, foo.z). Is...
5
by: jmd | last post by:
hello. i am trying VC++ 7.1 (with vsnet 2003). my example : class Complex { public: Complex ( float re_ = 0.0, float im_ = 0.0 ) : re(re_), im(im_) {} float Re () { return ( re ); } float...
7
by: Eric Lilja | last post by:
>From a book, I know the following is true for the comparison operators: An overloaded operator that is a class member is only considered when the operator is used with a *left* operand that is an...
4
by: tech | last post by:
Hi, I want to return a reference to member object provided in class Test, so have provided a getter function GetObj() and declared it const. However i'm getting a compiler error that i need to...
7
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.