小编ano*_*der的帖子

使用多重继承时,为什么这个限定名称不明确?

我正在尝试使用Bottom对象访问struct Top中的成员变量x.

代码如下:

#include <cstdio>

struct Top
{
public:
    int x = 1;
};

struct Left : public Top
{
    int x = 2;
};

struct Right : public Top
{
    int x = 3;
};

struct Bottom : public Left, public Right
{
    int x = 4;
}; 

int main()
{
    Bottom b;
    std::printf("value: %d\n", b.Left::Top::x);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这使用gcc 4.8给出以下错误:

main.cpp: In function 'int main()':
main.cpp:27:45: error: 'Top' is an ambiguous base of …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance multiple-inheritance

8
推荐指数
1
解决办法
199
查看次数

标签 统计

c++ ×1

inheritance ×1

multiple-inheritance ×1