Default copy operator on derivated classes

  • Thread starter Alexander Tumarov
  • Start date
A

Alexander Tumarov

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

Rob Williscroft

Alexander Tumarov wrote in @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::eek: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.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top