diamond inheritance hides non-default constructor

Discussion in 'C++' started by Tom, Oct 25, 2004.

  1. Tom

    Tom Guest

    Hello, I've searched groups and the std unsuccessfully for an explanation
    of the following - I'd appreciate any comments you may have.

    Given the following diamond multiple inheritance pattern, with the base
    class having 2 constructors, only the default constructor is called. Is
    there a way to require the other constructor to be used?

    First, the output:

    Base default constructor
    Derived1:
    Derived2:
    Join:
    Main:

    class Base
    {
    public:
    Base ()
    { cout << "Base default constructor" << endl; }
    Base (string const & useMsg)
    : msg (useMsg)
    { cout << "Base: " << msg << endl; }
    virtual ~Base() {}
    string msg;
    };

    class Derived1 : virtual public Base
    {
    public:
    Derived1 (string const & useMsg)
    : Base (useMsg)
    { cout << "Derived1: " << msg << endl; }
    };

    class Derived2 : virtual public Base
    {
    public:
    Derived2 ()
    : Base ()
    { cout << "Derived2: " << msg << endl; }
    };

    class Join : public Derived1, public Derived2
    {
    public:
    Join ()
    : Derived1 ( string ("Hello") ), Derived2 ()
    { cout << "Join: " << msg << endl; }
    };

    int main ()
    {
    Join join;
    cout << "Main: " << join.msg << endl;
    return 0;
    }
    Tom, Oct 25, 2004
    #1
    1. Advertising

  2. Tom

    JKop Guest


    > class Join : public Derived1, public Derived2
    > {
    > public:
    > Join ()
    > : Derived1 ( string ("Hello") ), Derived2 ()
    > { cout << "Join: " << msg << endl; }
    > };


    Join() : Derived1( "Hello" ), Derived2(), Base("this is what I was
    missing!")
    {

    }


    -JKop
    JKop, Oct 25, 2004
    #2
    1. Advertising

  3. Tom wrote:
    > Hello, I've searched groups and the std unsuccessfully for an explanation
    > of the following - I'd appreciate any comments you may have.
    >
    > Given the following diamond multiple inheritance pattern, with the base
    > class having 2 constructors, only the default constructor is called. Is
    > there a way to require the other constructor to be used?
    >
    > First, the output:
    >
    > Base default constructor
    > Derived1:
    > Derived2:
    > Join:
    > Main:
    >
    > class Base
    > {
    > public:
    > Base ()
    > { cout << "Base default constructor" << endl; }
    > Base (string const & useMsg)
    > : msg (useMsg)
    > { cout << "Base: " << msg << endl; }
    > virtual ~Base() {}
    > string msg;
    > };
    >
    > class Derived1 : virtual public Base
    > {
    > public:
    > Derived1 (string const & useMsg)
    > : Base (useMsg)
    > { cout << "Derived1: " << msg << endl; }
    > };
    >
    > class Derived2 : virtual public Base
    > {
    > public:
    > Derived2 ()
    > : Base ()
    > { cout << "Derived2: " << msg << endl; }
    > };
    >
    > class Join : public Derived1, public Derived2
    > {
    > public:
    > Join ()
    > : Derived1 ( string ("Hello") ), Derived2 ()
    > { cout << "Join: " << msg << endl; }
    > };
    >
    > int main ()
    > {
    > Join join;
    > cout << "Main: " << join.msg << endl;
    > return 0;
    > }



    Virtual base class initialisation is done at the level of the most derived
    class. In your case the 'Join' c-tor should have

    Base(???)

    in its initialiser list (along with other things). Replace the ??? with
    the appropriate expression you want to be evaluated and passed to the
    Base's c-tor. I suspect it will be

    Base("Hello")

    Victor
    Victor Bazarov, Oct 25, 2004
    #3
  4. Tom

    Tom Guest

    Thanks for your answers.
    I finally found your quote in the std doc under initialization (section 12),
    not in the section on inheritance (section 10). The explanation is in the
    sentence following your quote of it:

    All sub-objects representing virtual base classes are initialized by
    the constructor of the most derived class. If the constructor of
    the most derived class does not specify a mem-initializer for a
    virtual base class V, then V's default constructor is called to
    initialize the virtual base class subobject.

    Tom


    "Victor Bazarov" <> wrote in message
    news:Zl8fd.6857$09.us.to.verio.net...
    >
    > Virtual base class initialisation is done at the level of the most derived
    > class. In your case the 'Join' c-tor should have
    >
    > Base(???)
    >
    > in its initialiser list (along with other things). Replace the ??? with
    > the appropriate expression you want to be evaluated and passed to the
    > Base's c-tor. I suspect it will be
    >
    > Base("Hello")
    >
    > Victor
    Tom, Oct 26, 2004
    #4
    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. jeffc
    Replies:
    10
    Views:
    668
    lilburne
    Nov 6, 2003
  2. Aire
    Replies:
    3
    Views:
    456
    Mike Wahler
    Jan 25, 2004
  3. Replies:
    2
    Views:
    340
    Old Wolf
    Sep 12, 2005
  4. desktop
    Replies:
    2
    Views:
    449
    Victor Bazarov
    Jun 6, 2007
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,202
Loading...

Share This Page