我需要通过 A 的对象
a
将 x = 2000
分配给 B
的 x
这里B是派生类,即继承类A。
class A
{
public:
int x, y;
void print()
{
cout<<endl<<"print() of A";
}
virtual void display()
{
cout<<endl<<"display() of A";
}
};
class B: public A
{
public:
int x, z;
void display()
{
cout<<endl<<"display() of B";
}
void print()
{
cout<<endl<<"print() of B";
}
};
最佳答案
通过执行以下操作找到答案:
((B *)aptr)->x = 2000;
关于c++ - 如何使用基类对象设置派生类变量的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43474563/