c++ - 当父类和子类的数据成员同名时,如何从子类访问父类的数据成员

标签 c++ visual-c++ inheritance public

我的场景如下::

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/

相关文章:

c++ - 同时学习 Visual C++ 2008 和 C++?有什么资源可以推荐吗?

c++ - 在 C++ 中停止或阻止继​​承

c++ - 一段初学者 C++ 代码的模糊编译器错误

c++ - 为什么显式弃用 register 存储类

c++ - 为什么我得到 "recursive type or function dependency context too complex"?

c++ - 为什么我的 If/Else 语句 block 没有完全执行?

c++ - 使用 PSAPI 时出现 MFC 错误

c++ - 无法将字符串参数传递给派生类成员函数 C++

c++ - GetWindowText 无法处理商标符号

c++ - 错误 C2275 RHandle : illegal use of this type as an expression