当父类和子类具有相同的dat成员名称时,如何从子类访问父类的数据成员

cod*_*ver 10 c++ inheritance public visual-c++

我的情况如下::

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".  
}
}
Run Code Online (Sandbox Code Playgroud)

这里是如何从Child的成员函数访问Parent的"X".

Luc*_*ore 10

几乎得到了它:

this->x = Parent::x;
Run Code Online (Sandbox Code Playgroud)

this 是一个指针.

  • 不需要`this->`. (7认同)