C++ 相当于“超级”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4424216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:22:16  来源:igfitidea点击:

C++ equivalent of "super"?

c++

提问by jmasterx

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

在 Java 中,使用 super 代替范围运算符,例如:

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?

在 C++ 中是否有类似的东西,或者由于多重继承而在 C++ 中没有意义?

Thanks

谢谢

回答by Martin v. L?wis

回答by wqking

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.

Martin 链接中的 typedef 技巧工作得很好(这就是为什么 C++ 没有超级或继承的 keywork AFAIR 的部分原因。)我们唯一需要关心的是 typedef 应该在私有部分。不要把它放在 protected 或 public 部分,否则,派生类可能会错误地使用 typedef 来引用它的祖父而不是它的父。

回答by ?? Tiib

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

微软的编译器有(被 C++ 标准委员会拒绝)扩展__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.

编辑:超级可能会混淆代码的读者。由于 C++ 中的多重继承,最好更明确。多重继承已经很复杂了。AFAIK 讨论了 super 对模板的有用性,在意识到任何人都可以在需要时 typedef super 后冷静下来。

回答by icecrime

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

C++ 中没有这样的东西,尽管您可以提供自己的typedef

struct Derived : Base
{
    typedef Base super;
};