A useful addition to a derived class is a typedef
aliasing the base class as super
:
class Derived : public Base { private: typedef Base super; };
This allows you to use super
to call the base class constructor from the derived class:
Derived(int i, int j) : super(i), j_(j) { }
You can also use super
to call the base class version of a virtual function in a derived class override:
virtual void Show() { super::Show(); std::cout << "Derived Show()\n"; }
It means that if you need to add another layer of inheritance, you only need to change the inheritance part of the class declaration and this typedef
.
class Derived : public Intermediate { private: typedef Intermediate super; };
You should make the typedef
private, to prevent it from being inherited and accidentally used from derived classes that do not declare it.
Visual C++ has a non-standard extension __super
: __super.
It is available in Java: Using the Keyword super.
Nowadays you should probably use using
:
class Derived : public Base { private: using super = Base; };
So it should probably be called the "using super =" idiom.