Question

I am learning OO in C++ programming these days in VS2010. I meet with some basic Inheritance problems in C++. Here is my code:

Question 1:

class bs
{
public:
    int a;
    virtual void name(){};
};

class A:virtual public bs
{
public:
    int a;
    virtual void name(){};
};


class B:virtual public bs
{
public:
    int a;
    virtual void name(){};
};

class D:virtual public bs
{
public:
    int a;
    virtual void name(){};
};


class C:public A,public B,public D
{

};

The compiler gives me the error C2250. When I remove the virtual inheritance from each class. i.e.

class bs
{
public:
    int a;
    virtual void name(){};
};

class A:public bs
{
public:
    int a;
    virtual void name(){};
};


class B:public bs
{
public:
    int a;
    virtual void name(){};
};

class D:public bs
{
public:
    int a;
    virtual void name(){};
};


class C:public A,public B,public D
{

};

it takes effect. Also,if I rewrite the virtual void name(){}; in class C in the error code,the compiler goes well.So my question is why it is necessary to force to rewrite the virtual function in virtual inheritance situation,and unnecessary to non-virtual inheritance situation.

Question 2:

As the code above,when I want to add the bs *mybs = new C(); in the non-virtual inheritance situation

class bs
{
public:
    int a;
    virtual void name(){};
};

class A:public bs
{
public:
    int a;
    virtual void name(){};
};


class B:public bs
{
public:
    int a;
    virtual void name(){};
};

class D:public bs
{
public:
    int a;
    virtual void name(){};
};


class C:public A,public B,public D
{
    virtual void name(){};
};

//bs *mybs = new C(); //C2594

the compiler says error C2594. But when in virtual inheritance situation,the compiler has no error.

class bs
{
public:
    int a;
    virtual void name(){};
};

class A:virtual public bs
{
public:
    int a;
    virtual void name(){};
};


class B:virtual public bs
{
public:
    int a;
    virtual void name(){};
};

class D:virtual public bs
{
public:
    int a;
    virtual void name(){};
};


class C:public A,public B,public D
{
    virtual void name(){};
};

bs *mybs = new C();//It is OK

What's the matter in virtual inheritance situation?

I think these two questions are not difficult but basic in C++. However,I am not familiar with the OO in C++ version. Thank you for solving the problems.

Was it helpful?

Solution

Q1: given virtual inheritance, the derived class only gets one instance of the bs object, yet there are three implementations of name(), and it doesn't know which to use. If you derive non-virtually, then there are three bs bases embedded in the A, B and D classes, each of which can override the implementation. Alternatively, you have found that you can replace the ambiguous implementations after virtual inheritance with one implementation in C.

Q2: in the non-virtual situation, you have three bs bases in the embedded A, B and D objects: which of these do you want a pointer to? It's ambiguous so you get the error. If you used virtual inheritance, then there's only one bs base object and you can get a pointer to it.

To illustrate:

 VIRTUAL            NON-VIRTUAL
 .       bs          bs  bs  bs
 .     /  |  \       |   |   |
 .     A  B  D       A   B   D
 .     \  |  /        \  |  /
 .        C              C

On the left, your problem was C didn't know which of A::name(), B::name() or D::name() to use unless you overrode all three in C.

On the right, your problem was that b* my_b_ptr didn't know which bs base to point at.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top