15.5 class scopes under Inheritance
In the case of inheritance, the scope of the derived class is nested in the scope of the base class. If the name cannot be determined in the scope of the derived class, search for the definition of the name in the scope of the peripheral base class.
15.5.1 Name Lookup occurs during compilation
The static types of objects, references, or pointers determine the actions that an object can perform. Even when the static type and the dynamic type may be different, just as the reference or pointer of the base class type may occur, the static type still determines what Members can be used.
15.5.2 Name Conflict and inheritance
A derived class member with the same name as a base class member will block basic access to the base class member.
Class Base
{
Protected:
Int value1;
Public:
Base (int val1): value1 (0 ){}
};
Class Child: public Base
{
Private:
Int value1;
Public:
Child (int val1): value1 (val1), Base (val1 ){}
Int GetValue1 ()
{
Return value1;
}
};
Child c (11 );
Cout <c. GetValue1 () <endl; // 11
Use the scope operator to access the blocked Member
You can use the scope operator to access the blocked base class members.
Int GetValue1 ()
{
Return Base: value1;
}
When designing a derived class, it is best to avoid conflicts with the names of base class members as long as possible.
From xufei96's column