c++ - 不可用的公共(public)变量

标签 c++ class

<分区>

class A()
{};

class B : public A()
{public:int x};
int main()
{
 A* test = new B();
 test->x = 1;
 return 0;
}

为什么 x 不可用?我该如何解决?谢谢。

最佳答案

因为编译器只检查类 A 的方法/成员中的 x。您已将其声明为 A,但它确实需要是 B,或者在您使用它时需要强制转换为 B。

另外,这是作业吗?这似乎是做作的。

关于c++ - 不可用的公共(public)变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29186265/

相关文章:

c++ - 模板与继承,如何调用基础虚方法

c++ - 在 .cpp 中的类主体外部定义内联函数

在 Xcode 中运行的 C++ 程序,但不在 Eclipse 中

C++/W32 Sharing Class - 代码设计题

javascript - 在 React 组件或该文件中定义函数的优点和缺点

vb.net - 错误: "Expression recursively calls the containing property ' length'"mean?是什么意思

c++ - COM 调度接口(interface)事件

c++ - 如何将 std::vector<KeyPoint> 保存到 C++ 中的文本文件

C# - 动态生成的类?

python - 在 __init__ 中获取有序字典类属性