Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Odd behavior of operator=() in diamond inheritance situation

Reply
Thread Tools

Odd behavior of operator=() in diamond inheritance situation

 
 
Juha Nieminen
Guest
Posts: n/a
 
      03-01-2009
Consider the following code:

//---------------------------------------------------------------
#include <iostream>

struct Base
{
Base() { std::cout << "Base constructor\n"; }
Base(const Base&) { std::cout << "Base copy constructor\n"; }
Base& operator=(const Base&)
{ std::cout << "Base operator=\n"; return *this;}
};

struct M1: virtual public Base {};
struct M2: virtual public Base {};
struct Derived: public M1, public M2 {};

int main()
{
Derived d1;
Derived d2(d1);
d1 = d2;
}
//---------------------------------------------------------------

One would assume that this program prints one message of each type.
However, a bit surprisingly, this is the result:

Base constructor
Base copy constructor
Base operator=
Base operator=

Why is that?

I'm using gcc 4.3.1.
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      03-02-2009
On Mar 2, 1:50 am, "Victor Bazarov" <(E-Mail Removed)> wrote:
> Juha Nieminen wrote:
> > Consider the following code:


> > //---------------------------------------------------------------
> > #include <iostream>


> > struct Base
> > {
> > Base() { std::cout << "Base constructor\n"; }
> > Base(const Base&) { std::cout << "Base copy constructor\n"; }
> > Base& operator=(const Base&)
> > { std::cout << "Base operator=\n"; return *this;}
> > };


> > struct M1: virtual public Base {};
> > struct M2: virtual public Base {};
> > struct Derived: public M1, public M2 {};


> > int main()
> > {
> > Derived d1;
> > Derived d2(d1);
> > d1 = d2;
> > }
> > //---------------------------------------------------------------


> > One would assume that this program prints one message of
> > each type. However, a bit surprisingly, this is the result:


> > Base constructor
> > Base copy constructor
> > Base operator=
> > Base operator=


> > Why is that?


> The Derived's assignment operator (provided by the compiler)
> calls two assignment operators for each base class. They, in
> turn, call its base class' assignment operator. If you add
> the printout of the 'this' pointer in the Base:perator=, you
> will see that it is the same object that is being assigned
> twice. No big deal.


That's doubtlessly the correct explination of what is happening,
but it's not guaranteed. According to the standard: "It is
unspecified whether subobjects representing virtual base classes
are assigned more than once by the implicitly-defined copy
assignment operator."

In practice, of course, most of the time, the virtual base will
be abstract, and not contain any data members, so it won't
matter. Most of the time.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
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
Odd situation or maybe not. Drew Windows 64bit 3 05-02-2012 11:37 PM
Odd behavior with odd code Michael Speer C Programming 33 02-18-2007 07:31 AM
diamond inheritance hides non-default constructor Tom C++ 3 10-26-2004 02:39 PM
virtual inheritance / dreaded diamond again Alexander Stippler C++ 1 08-26-2003 02:53 PM
virtual inheritance / dreaded diamond problem Alexander Stippler C++ 0 07-14-2003 04:52 PM



Advertisments