简体   繁体   中英

c++ multiple inheritance

I cannot figure out what would happen in the following scenario:

class MBase {
    public:
       MBase(int) {}
       virtual char* vf() const = 0;
       virtual ~MBase() {}
};

class D1 : public MBase { //NOT VIRTUAL!!!
   public:
   D1() : MBase(1) {}
   char* vf() const { return "D1"; }
};

class D2 : virtual public MBase {
    public:
        D2() : MBase(2) {}
        char* vf() const { return "D2"; }
};

class Bottom : public D1, public D2 {
  public:
    char* vf() const { return "Bottom"; }
}

Base* b = new Bottom();  

In the original definition of the diamond both D1 and D2 were inheriting virtually from MBase, but here only one is. Would we still have two separate subobjects in Bottom object and therefore, the last line will not compile as compiler won't know which subobject to use?

This is specified in section 10.1.4 of the C++03 standard. Virtual and non-virtual bases are independent, so there will be one of each.

It is easy to see this by expanding your example:

class MBase {
    public:
       MBase(int arg) { cerr << "MBase::MBase(" << arg << ")\n"; }
       virtual const char* vf() const = 0;
       virtual ~MBase() {}
};

class D1 : public MBase { //NOT VIRTUAL!!!
   public:
   D1() : MBase(1) {}
   const char* vf() const { return "D1"; }
};

class D2 : virtual public MBase {
    public:
        D2() 
        : MBase(2) // This doesn't get used in this example because
                   // it is the responsibility of the most-derived
                   // class to initialize a virtual base, and D2 isn't
                   // the most-derived class in this example.
        {
        }
        const char* vf() const { return "D2"; }
};

class Bottom : public D1, public D2 {
  public:
    Bottom() 
    : MBase(5) // D1 and D2 default constructors are called implicitly.
    { 
    }
    const char* vf() const { return "Bottom"; }
};

int main(int argc,char **argv)
{
  Bottom b;
  return 0;
}

Outputs:

MBase::MBase(5)
MBase::MBase(1)

I think you problem is similar to this one... Basically, each objet has their own base object of mBase class...

Diamond inheritance

Regards, Erwald

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM