小编Joh*_*ano的帖子

为什么 x 成员变量不明确?

有人可以向我解释以下编译器错误,该错误表示“x”是一个不明确的引用吗?

如果编译器知道其中一个变量实际上无法访问,为什么它不允许这样做?

class A {
    int x; // private here
};

class B {
public:
int x; // public here
};

class C : public A, public B {

};

int main() {

    C c;
    c.x = 5; // here is the error

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编辑:对于向我解释私有并不意味着它不能更改的人 - 我知道这一点,并在下面做了这个简单的例子,但这不是我要问的情况。

//
// the goal is to hack x, y values
//
#include <stdio.h>
#include <memory>

class A {
    int x;
    int _r;
    double y;
public:
    A() { x = 1; y …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance multiple-inheritance

5
推荐指数
1
解决办法
621
查看次数

标签 统计

c++ ×1

inheritance ×1

multiple-inheritance ×1