Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ equivalent of "super"? [duplicate]

Tags:

c++

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?

Thanks

like image 397
jmasterx Avatar asked Dec 12 '10 21:12

jmasterx


4 Answers

There is a convention of defining a super typedef in every class.

like image 155
Martin v. Löwis Avatar answered Nov 10 '22 17:11

Martin v. Löwis


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.

like image 33
Öö Tiib Avatar answered Nov 10 '22 17:11

Öö Tiib


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.

like image 6
wqking Avatar answered Nov 10 '22 17:11

wqking


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

struct Derived : Base
{
    typedef Base super;
};
like image 5
icecrime Avatar answered Nov 10 '22 19:11

icecrime