Convention for accessing inherited members eases development.

Index

When you are using a command prompt and wish to change to the "parent directory," you typically will type something along the lines of "cd ..". Unfortunately, C++ offers no simple way of achieving the same feature when dealing with classes. This tip explains a simple convention for accessing inherited members through a relative naming scheme.

The gist of the idea is to always use typedef to create an alias for your base class, and to always use the same alias. For example:

class base
{
base( base* owner )
};

class derived : public base
{
typedef base inherited;
derived( base* owner );
}

derived::derived( base* owner )
: inherited( owner )
{ }

Why would you go through the trouble, you ask? Well, suppose your class library is evolving. Shortly after implementing a series of members in derived which all use services provided by base, you realize you need to have an intermediate class, intermediary which modifies the behavior of base. If you had referenced base directly in all of the members of derived, you would have to find and replace every single instance of the token base with the token intermediary. If, however, you had used inherited to access base's members, then you would only need to change the declaration of derived and of derived::inherited, like so:

class base
{
base( base* owner );
};

class intermediary : public base
{
intermediary( base* owner );
};

class derived : public intermediary
{
typedef intermediary inherited;
derived( base* owner );
}

derived::derived( base* owner )
: inherited( owner )
{ }

Obviously, this sample is overly simplified and the convention described would not be very helpful. However, in more complex settings this technique has proved quite useful.

typedef xxx inherited FAQs