我正在尝试使用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)