Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why is not copy constructor called?

Reply
Thread Tools

Why is not copy constructor called?

 
 
janzon@gmail.com
Guest
Posts: n/a
 
      10-13-2006

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

 
Reply With Quote
 
 
 
 
Pete C
Guest
Posts: n/a
 
      10-13-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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.

 
Reply With Quote
 
 
 
 
IR
Guest
Posts: n/a
 
      10-13-2006
(E-Mail Removed) wrote:

> 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,

--
IR
 
Reply With Quote
 
IR
Guest
Posts: n/a
 
      10-13-2006
IR wrote:

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


dammit i forgot return *this;
shame on me

--
IR
 
Reply With Quote
 
gt8887b
Guest
Posts: n/a
 
      10-13-2006
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();



On Oct 13, 6:29 pm, (E-Mail Removed) wrote:
> 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;
>
>
>
> }- Hide quoted text -- Show quoted text -


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-13-2006
* (E-Mail 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


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
IR
Guest
Posts: n/a
 
      10-13-2006
Alf P. Steinbach wrote:

> 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.

--
IR
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-13-2006
* IR:
> Alf P. Steinbach wrote:
>
>> 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?


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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
IR
Guest
Posts: n/a
 
      10-13-2006
Alf P. Steinbach wrote:

> 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

--
IR
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-14-2006
* 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++!

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
template copy constructor vs normal copy constructor cinsk C++ 35 10-10-2010 11:14 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Deep Copy smart pointer not requiring virtual copy constructor Nindi73@yahoo.co.uk C++ 11 11-14-2006 02:23 PM
Copy constructor: why can't I copy objects as if they were structs? rdc02271 C++ 24 12-27-2005 12:38 PM



Advertisments