开发者

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

This question already has answers here: Using "super" in C++ 开发者_JAVA百科 (19 answers) Closed 9 years ago.

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


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


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

struct Derived : Base
{
    typedef Base super;
};


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜