Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Default copy operator on derivated classes

Reply
Thread Tools

Default copy operator on derivated classes

 
 
Alexander Tumarov
Guest
Posts: n/a
 
      05-01-2004
I tried to run next code compiles with g++

//--------------------------------
#include <stdio.h>

class B
{
public:
B& operator=(const B &b)
{
printf("BBBBBBBB \n");
return *this;
};
};

class C
{
public:
C& operator=(const C &c)
{
printf("CCCCCCC \n");
return *this;
};
};

class D : public B,public C
{
public:
D& operator=(const C &c)
{
printf("DDDDDD \n");
return *this;
};
};



int main(int argc, char* argv[])
{
C c1;
D d1,d2;

d1=d2;
d1=c1;

return 0;
}

// --------------------------

And got next output

BBBBBBBB
CCCCCCC
DDDDDD

The question is why compiler builds call to both
B& operator=(const B &b)
C& operator=(const C &c)

on the line
d1=d2;
???

A much as I remember on the case of missing copy operator the compiler
should build default one that performs bit-to-bit copy and not call base
copy operators. Am I wrong?
Can anybody point me to the specific paragraph in the ANSI standard?

Thank you.
 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      05-01-2004
Alexander Tumarov wrote in news:3ca86be5.0405010219.4ceceb05
@posting.google.com in comp.lang.c++:

>
> The question is why compiler builds call to both
> B& operator=(const B &b)
> C& operator=(const C &c)
>
> on the line
> d1=d2;
> ???
>
> A much as I remember on the case of missing copy operator the compiler
> should build default one that performs bit-to-bit copy and not call base
> copy operators. Am I wrong?


Yes you're wrong, the compiler generated D:perator=(D const &) does
member (and basewise) assignment using the members/bases (possibly
compiler generated) operator=. The compiler never does bit-to-bit
copying of a UDT, except perhaps under the as-if rule, when all
members are builtins and use (or can use) bit-to-bit copying.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
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
Why derivated exception can not be pickled ? Mathieu Courtois Python 5 09-05-2012 10:18 AM
Property persistence in derivated control Ornette ASP .Net Web Controls 0 09-15-2008 09:32 PM
conditions for automatic generation of default ctor, copy ctor,and default assignment operator (operator) puzzlecracker C++ 8 04-15-2008 09:56 PM
boost.thread - class derivated from thread Lars Uffmann C++ 13 02-19-2008 09:34 AM
Dynamic decide derivated obj firegun9@yahoo.com.tw C++ 4 05-26-2005 02:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57