tags:

views:

69

answers:

1

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? 

}
+8  A: 

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.

James McNellis
+1 for answering the question as asked and pointing out that it's generally not a good idea to do so.
Mark B