Search

Recent

Tags

Using “super” in C++

Filed under C++, tips & tricks on jan 06, 2010

My style of coding includes the following idiom:

class Derived : public Base
{
private:
  typedef Base super;
  // etc...
};

This enables me to use “super” as an alias to Base, for example, in constructors:

Derived(int i, int j) : super(i), J(j)
{
}

Or even when calling the method from the base class inside its overriden version:

void Derived::doSomething()
{
  super::doSomething() ;
  // etc...
}

It can even be chained (I have still to find the use for that, though):

class DerivedDerived : public Derived
{
public:
  typedef Derived super;
  // etc...
};

void DerivedDerived::doSomethingElse()
{
  super::doSomethingElse() ; // will call Derived::doSomethingElse()
  super::super::doSomethingElse() ; // will call Base::doSomethingElse()

  // etc...
}

Anyway, I find the use of “typedef super” very useful, for example, when Base is either verbose and/or templated.

Add to

Post your feedback

You can use this form to leave your feedback. Your insights are always appreciated.

Tools

View document source in text/plain