Name hiding with inheritance

If a name is used in a parent class and then redefined in a derived class, the name in the parent class is hidden by that in the derived class. Code in the parent class continues to use the meaning defined there, but code in the derived class sees the redefinition.

If you access an object of the derived class through a pointer of the parent class type, you will get the names defined in the parent class. This is because the compiler cannot tell at runtime whether a parent class pointer refers to a sub-class. The only safe assumption is that the object is of the parent class.

This is usually referred to as static binding. We shall see later how to make sub-class definitions visible through a parent class pointer, when we consider virtuals.

Next note in series

Back to index