473,386 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Default copy operator on derivated classes

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.
Jul 22 '05 #1
1 1369
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::operator=(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/
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
4
by: away | last post by:
1. When a class defined with a member of pointer type, it's necessary to have a copy constructor and assignment operator. If don't pass objects of such class as value in a function and don't do...
4
by: moleskyca1 | last post by:
Hi, In a recent discussion, some of us were in disagreement about the functions the C++ compiler generates. How many functions are generated by the compiler when you declare: class Foo { };...
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
2
by: BeautifulMind | last post by:
Hi, As per the C++ standard, after the compilation of the following class "Test" how many / which members would be generated by the compiler. and actually I am also interested behind the logic...
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
9
by: George2 | last post by:
Hello everyone, I am wondering the default implementation of assignment operator (e.g. when we do not implement assignment operator in user defined class, what will be returned? temporary...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.