我的场景如下::
class Parent
{
public:
int x;
}
class Child:public Parent
{
int x; // Same name as Parent's "x".
void Func()
{
this.x = Parent::x; // HOW should I access Parents "x".
}
}
这里是如何从 Child 的成员函数访问 Parent 的“X”。
最佳答案
差不多明白了:
this->x = Parent::x;
this
是一个指针。
关于c++ - 当父类和子类的数据成员同名时,如何从子类访问父类的数据成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11525418/