How to access parent class's data member from child class, when both parent and child have the same name for the dat member

前端 未结 3 2577
粉色の甜心
粉色の甜心 2021-02-20 18:17

my scenario is as follows::

class Parent
{
public:
int x;
}

class Child:public Parent
{
int x; // Same name as Parent\'s \"x\".

void Func()
{
   this.x = Paren         


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-02-20 19:05

    It's only a brief explaination of solutions provided by Luchian Grigore and Mr. Anubis, so if you are curious 'how this works', you should read it further.

    C++ provides a so-called, "scope operator" (::), which is perfectly suited to your task.

    More details are provided at this page. You can combine this operator with class name (Parent) to access parent's x variable.

提交回复
热议问题