Using the Scope Operator to Use Hidden Members
We can use a hidden base-class member by using the scope operator:
struct Derived : Base { int get_base_mem() { return Base::mem; } // ...};
The scope operator overrides the normal lookup and directs the compiler to look for mem
starting in the scope of class Base
. If we ran the code above with this version of Derived
, the result of d.get_mem()
would be 0
.
Best Practices
Aside from overriding inherited virtual functions, a derived class usually should not reuse names defined in its base class.
Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.