简体   繁体   中英

Inherit from two parent class (which inherit from one base class) but some methods don't work

i have troubles with some methods after inherit. It's hard (for me) to say where exactly problem is but i will try to expose this by example.

Minimal code:

#include <iostream>

class A
{
public:
    A() {};
    A(int x):val(x)
    {
        std::cout << "A constructor work" << std::endl;
    }

    int get()
    {
        std::cout << "Get A work" << std::endl;
        return val;
    }

protected:
    int val; 
};

class B: protected A
{
public:
    B(int x) :A(x) 
    {
        std::cout << "B constructor work" << std::endl;
        test();
    }

    int get()
    {
        std::cout << "Get B work" << std::endl;
        return A::get();
    }

protected:
    void test()
    {
        if (A::val == 0)
        {
            std::cout << "Test B work" << std::endl;
            A::val = 1;
        }
    }

};

class C : protected A
{
public:

    C() {};
    C(int x) :A(x)
    {
        std::cout << "C constructor work" << std::endl;
        test();
    }

    int get()
    {
        std::cout << "Get C work" << std::endl;
        return A::get();
    }
protected:
    void test()
    {
        std::cout << "Test C work" << std::endl;

        if (A::val != 0)
        {
            A::val += 2;
        }
    }

};

class D : private B, private C
{
public:
    D(int x):B(x)
    {
        std::cout << "D constructor work" << std::endl;
        C::test();
    }

    int get()
    {
        std::cout << "Get D work" << std::endl;
        return B::get();
    }

};


int main()
{
    D d(0);

    std::cout << d.get() << std::endl;
}


Output: 
**A constructor work
B constructor work
Test B work
D constructor work
Test C work
Test C extra work
Get D work
Get B work
Get A work
1**

I expect val = 3 in the end, but it dont work like that. I would be particularly grateful for your detailed reply.

Your class D contains two A objects - the one inherited by B and the one inherited by C

When you call C::test() you change the A object in C .

When you call D::get() - which calls B::get() - you inspect the value of the A object in B .


One way to get around this is by means of virtual inheritance . That is not something you want to mess with until you understand how multiple inheritance works, though.

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