Odd behavior of operator=() in diamond inheritance situation

Discussion in 'C++' started by Juha Nieminen, Mar 1, 2009.

  1. 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.
    Juha Nieminen, Mar 1, 2009
    #1
    1. Advertising

  2. Juha Nieminen

    James Kanze Guest

    On Mar 2, 1:50 am, "Victor Bazarov" <> 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::eek: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:
    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
    James Kanze, Mar 2, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander Stippler

    virtual inheritance / dreaded diamond problem

    Alexander Stippler, Jul 14, 2003, in forum: C++
    Replies:
    0
    Views:
    1,867
    Alexander Stippler
    Jul 14, 2003
  2. Alexander Stippler

    virtual inheritance / dreaded diamond again

    Alexander Stippler, Aug 26, 2003, in forum: C++
    Replies:
    1
    Views:
    380
    Ron Natalie
    Aug 26, 2003
  3. Tom
    Replies:
    3
    Views:
    492
  4. John Perks and Sarah Mount

    MRO problems with diamond inheritance?

    John Perks and Sarah Mount, May 1, 2005, in forum: Python
    Replies:
    13
    Views:
    585
    Michele Simionato
    May 3, 2005
  5. Michael Speer

    Odd behavior with odd code

    Michael Speer, Feb 16, 2007, in forum: C Programming
    Replies:
    33
    Views:
    1,062
    Richard Heathfield
    Feb 18, 2007
Loading...

Share This Page