开发者

How to access member with same name in the inheritance

开发者 https://www.devze.com 2022-12-22 00:53 出处:网络
I have a question about how to access the member with the same name with inheritance. For example, class Base {

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

精彩评论

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