开发者

How to access member with same name in the inheritance

I have a question about how to access the member with the same name with inheritance. For example,

class Base { 

public:
int i;  

};
class Derived1 : public Base {

    public:
    int i;

    // how to access the i in the base class here?
};

int main() {

  Derived1 d;
  cout<<d.i;                          //which is it is?

  //how to access the different开发者_高级运维 i here? 

}


d.i in your example refers to the i in the derived class.

You can refer to the base class i by qualifying it with the base class name:

d.Base::i

In general, it's a bad idea to have derived classes with members having the same name as members in base classes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜