Item-43 Know how to access names in templatized base classes
· β 5 min read
In derived class templates, refer to names in base class templates via a this->
prefix, via using
declarations, or via an explicit base class qualification.
In derived class templates, refer to names in base class templates via a this->
prefix, via using
declarations, or via an explicit base class qualification.
When declaring template parameters, both class
and typename
are interchangeable. When identifying nested dependent type names, use typename
, except in base class lists or as a base class identifier in a member initialization list.
Classes support explicit interfaces based on function signatures, as well as runtime polymorphism through virtual functions; templates support implicit interfaces based on valid expressions, as well as compile-time polymorphism through template instantiation and function overloading resolution.
Being more complex than single inheritance, Multiple inheritance (MI) can lead to ambiguity issues and to the need for virtual inheritance, the latter of which imposes costs in size, speed, and complexity of initialization and assignment, so it’s more practical to make “empty” virtual base classes.
Private inheritance means is-implemented-in-terms-of. It is usually inferior to composition, but it makes sense when a derived class needs access to protected base members or needs to redefine inherited virtual functions. For library developers who strive to minimize object sizes, it also offers the ability of empty base optimization.