Why is not copy constructor called?

J

janzon

Consider the code below. The output is the following two lines:

0xbfc78090
0xbfc780a0

This proves that the variable m in main() is not the very same instance
of MyClass as temp_m in hello(). Hence (?) m is created as copy of
temp_m. But the copy constructor is not called. Contradiction. Where am
I thinking incorrectly?

#include <iostream>

using namespace std;

class MyClass
{
public:
MyClass () {}

MyClass (const MyClass& m)
{
cout << "Copy constructor called!" << endl;
}
};

MyClass hello()
{
MyClass temp_m;
cout << &temp_m << endl;
return temp_m;
}

int main()
{
MyClass m;
m=hello();
cout << &m <<endl;
}
 
P

Pete C

Consider the code below. The output is the following two lines:

0xbfc78090
0xbfc780a0

This proves that the variable m in main() is not the very same instance
of MyClass as temp_m in hello(). Hence (?) m is created as copy of
temp_m. But the copy constructor is not called. Contradiction. Where am
I thinking incorrectly?

m is not created as a copy of temp_m. It is default constructed, and
only later is it assigned to the (temporary) return value of hello(),
using the compiler-generated assignment operator. To see this, add the
following to your class definition:

MyClass &operator=(const MyClass& m)
{
cout << "Assignment operator called!" << endl;
return *this;
}

You will see that it is being called. Alternatively, try:

MyClass m(hello());

and you will see the results you probably expected the first time
around.
 
I

IR

This proves that the variable m in main() is not the very same
instance of MyClass as temp_m in hello(). Hence (?) m is created
as copy of temp_m. But the copy constructor is not called.
Contradiction. Where am I thinking incorrectly?
m=hello();

You are not using the copy constructor, but the assignment operator.

Try defining in MyClass

MyClass& operator =(const MyClass& m)
{
cout << "Assignment operator called!" << endl;
}

OR in main() (to use copy constructor) :

MyClass m(hello());

Cheers,
 
G

gt8887b

The code below should not call copy constructor.

Let's consider you main() again:

int main()
{
MyClass m; //here you construct an instance of MyClass - m
//the default constructor is used

//here you first call hello() which constructs another instance of
MyClass (temp_m)
//using the default constructor and returns it. The compiler would
usually avoid constructing
//a temporary MyClass object to be returned.
//
//next step is to assign the return value from hello() to m
//compiler would use the assignment operator rather then copy
constructor in this case
m=hello();
 
A

Alf P. Steinbach

* (e-mail address removed):
Consider the code below. The output is the following two lines:

0xbfc78090
0xbfc780a0

This proves that the variable m in main() is not the very same instance
of MyClass as temp_m in hello(). Hence (?) m is created as copy of
temp_m. But the copy constructor is not called. Contradiction. Where am
I thinking incorrectly?

#include <iostream>

using namespace std;

class MyClass
{
public:
MyClass () {}

MyClass (const MyClass& m)
{
cout << "Copy constructor called!" << endl;
}
};

MyClass hello()
{
MyClass temp_m;
cout << &temp_m << endl;
return temp_m;
}

int main()
{
MyClass m;
m=hello();
cout << &m <<endl;
}

Well, there are two possible misconceptions involved.

First, the '=' in the statement 'm=hello();' is an /assignment/,
invoking the assignment operator, not the copy constructor. The
difference between assignment and copy construction is that assignment
changes the member values of some existing object (perhaps deallocating
already allocated memory), whereas copy construction turns a chunk of
raw, uninitialized memory into an object, a copy. Perhaps you knew
that, but the formulation "creates as a copy of" seems to indicate that
this is indeed the basic misconception; if you'd written 'MyClass m =
hello();' then the "=" would instead denote copy construction.

Second, the compiler is allowed to optimize away copy construction in
certain situations, /regardless of whether the copy constructor has side
effects or not/. The copy constructor is very very special, in that the
compiler, in these relevant situations, is allowed to assume that what
the copy constructor does is to actually construct a perfect copy, and
nothing else whatsoever. These situations include the 'MyClass m =
hello();' initialization, as well as the call to 'hello()' itself (where
this optimization is known as RVO, Return Value Optimization).

This also means that C++ tests that ask you to count the number of copy
constructor calls in a piece of code, ending up with some exact number,
are generally tests made by incompetents (although in some pieces of
code you can be sure of the number of calls).

And unfortunately that includes most C++ tests, even some that cost $$$.

Hth.,

- Alf
 
I

IR

Alf said:
if you'd written 'MyClass m = hello();' then
the "=" would instead denote copy construction.

Interesting... I believe I once read an article on this topic, which
conclusion (as far a I recall) was:

Always use explicit copy construction
MyClass m(hello());
rather than assignment
MyClass m = hello();
as the latter translates to
MyClass m; m = hello();
which obviously calls an useless default constructor.

Alf (or anyone else), could you shed some light on this? Your
statement kinda confuses me, is this explicitely part of the standard?

Thanks in advance.
 
A

Alf P. Steinbach

* IR:
Interesting... I believe I once read an article on this topic, which
conclusion (as far a I recall) was:

Always use explicit copy construction
MyClass m(hello());
rather than assignment
MyClass m = hello();
as the latter translates to
MyClass m; m = hello();
which obviously calls an useless default constructor.

Alf (or anyone else), could you shed some light on this?

Yes, I did. ;-)

An initialization with "=" is formally a copy initialization, using the
copy constructor (which call can, however, be optimized away).

It's not translated to default construction plus assignment: a compiler
that did that (none such exist as far as I know) would be in flagrant
breach of the rules of the standard.

Your
statement kinda confuses me, is this explicitely part of the standard?

Yes.
 
I

IR

Alf said:
An initialization with "=" is formally a copy initialization,
using the copy constructor (which call can, however, be optimized
away).

It's not translated to default construction plus assignment: a
compiler that did that (none such exist as far as I know) would be
in flagrant breach of the rules of the standard.

Oh well, I just tested it in VS8. You are right (of course).

So, either the author of the article was wrong, or I have the memory
of a goldfish (which is the most likely of the two alternatives...)

In other words, my bad.
At least I'll have learned something today :)
 
A

Alf P. Steinbach

* IR:
[...], or I have the memory
of a goldfish (which is the most likely of the two alternatives...)

See e.g. <url: http://www.abc.net.au/science/k2/moments/s1179348.htm>:
<quote>
The fish that knew their tank remembered the trawling net so well,
that they could escape it in a follow-up study some 11 months later.

By the way, 11 months is nearly one third of his fish's 3-year
lifespan. That's a very long time to remember something that has
happened to you only once, and in human terms, about 25 years ago.

Yoichi Oda of Osaka University in Japan has spent years studying the
fine details of memory in goldfish - and he's also convinced that
goldfish have a good memory.
</quote>

See also e.g. Yahoo! Answers "How intelligent are goldfish?" at <url:
http://uk.answers.yahoo.com/question/index?qid=20061005044222AA8AcA0>:
<quote>
Goldfish and the 3 second memory span:
The Myth Busters TV show proved this one a myth. They taught fish to
swim through a maze, showing that their time got faster and faster.
Thus proving they remembered where the holes were.
</quote>

Thus, in conclusion, if you actually have the memory (that would be
analogous to the memory) of a goldfish, then you have a very good memory
indeed, able to remember trivial details a third of a lifespan later.

Anyway, let's stop the goldfish bashing in clc++!
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top