26

In Java, instead of using the scope operator, super is used ex:

C++ -> GenericBase::SomeVirtualFunction();
Java -> super.someVirtualMethod();

Is there something like this in C++ or does this not make sense in C++ because of multiple inheritance?

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
jmasterx
  • 52,639
  • 96
  • 311
  • 557

3 Answers3

6

There's no such thing in C++, although you can provide your own typedef :

struct Derived : Base
{
    typedef Base super;
};
icecrime
  • 74,451
  • 13
  • 99
  • 111
  • 1
    See the answer by @wqking that recommends putting the typedef in the `private:` section. – aldo Jul 10 '14 at 17:43
6

Microsofts compilers have (rejected by C++ standard commitee) extension __super.

Edit: Super may confuse readers of code. Because of multiple inheritance in C++ it is better to be more explicit. Multiple inheritance is already complex. There was AFAIK discussion about usefulness of super for templates that calmed down after it was realized that anyone can typedef super if they need it.

Öö Tiib
  • 10,809
  • 25
  • 44
  • 1
    Any comments on why the standards committee rejected this? (I didn't realize it was even up for consideration...) – Karl Knechtel Dec 12 '10 at 22:08
  • 3
    I also use the __super keyword, helps a lot if you're changing the base classes and stuff. Especially in MFC. Shame it's not standard, a very handy feature. – Coder Dec 12 '10 at 22:51
  • Just might convented it to be the first inherited class. Anyway everyone inherit interfaces after. That seems was emotional decision. – Sergei Krivonos Jul 25 '18 at 14:15
6

The typedef trick in Martin's link works quite well (and that's partial reason that's why C++ doesn't have super or inherited keywork AFAIR.) The only thing we need to care about is that the typedef should be in private section. Don't put it in protected or public section, otherwise, an derived class may wrong use the typedef to refer to its grandparent rather than its parent.

wqking
  • 228
  • 1
  • 5