1

我的理解是,在 E 对象中,C 和 D 对象分别由 c 和 d 引用。但我无法理解为什么 d.set_c('b') 无法将 B.m_c 初始化为 'b',因为 c.set_n(3) 能够将 A.m_n 的值更改为 3。

    #include <iostream>

    class A
    {
    public:
        A(int n = 2) : m_n(n) {}

    public:
        int get_n() const { return m_n; }
        void set_n(int n) { m_n = n; }

    private:
        int m_n;
    };

    class B
    {
    public:
        B(char c = 'a') : m_c(c) {}

    public:
        char get_c() const { return m_c; }
        void set_c(char c) { m_c = c; }

    private:
        char m_c;
    };

    class C
        : virtual public A
        , public B
    { };

    class D
        : virtual public A
        , public B
    { };

    class E
        : public C
        , public D
    { };

    int main()
    {
        E e;  //object of E is created
        C &c = e; //c is used to refrence C object in E Object 
        D &d = e; //c and d has same inheritance structure 
        std::cout << c.get_c() << d.get_n();

        c.set_n(3);
        d.set_c('b');
        std::cout << c.get_c() << d.get_n() << std::endl;

        return 0;
    }
4

3 回答 3

3

让我们看看你的类结构,如果你创建一个 的实例E,你最终会得到一个像这样的对象层次结构:

 class B   class A   class B
     \       / \       /
      \     /   \     /
       \   /     \   /
      class C   class D
          \       /
           \     /
            \   /
           class E

您会看到有两个实例B,但只有一个实例A。这是因为虚拟继承。两者都C继承DA使用虚拟继承,因此A您的e.

现在让我们看看用户代码:

E e;
C &c = e; // reference C in the object hierarchy of E
D &d = e; // reference D in the object hierarchy of E

c.set_n(3); // set the value in the only instance of A in E
d.set_c('b'); // set the value of one of the two instances of B in your e

std::cout << c.get_c(); // print the value in the other instance of B in e
                        // not the one you set before
std::cout << d.get_n(); // print the value of the only instance of A in e
                        // this got changed before
于 2013-01-31T10:49:13.893 回答
1

类的对象E包含子对象,CD子对象又包含一个B子对象。由于不涉及虚拟继承B,因此每个都C包含D 自己的B.

d.set_c()then 指的是Bcontains inD并更新 that,whilec.get_c()指的是Bcontains in C,它独立于 in D,因此没有改变。

对于类A及其成员n,由于使用了虚拟继承,因此没有此类问题。这确保了在类的最终对象中,每个人E都只A使用一个子对象,无论通过哪个“路径”访问它。

于 2013-01-31T10:50:13.693 回答
0

改变

, public B

, virtual public B

得到输出a2b3

在您的示例中,E实例包含单独的B实例。

于 2013-01-31T10:47:03.637 回答