在C ++中使用“超级”(Using “super” in C++)

编程入门 行业动态 更新时间:2024-10-12 01:30:58
在C ++中使用“超级”(Using “super” in C++)

我的编码风格包括以下成语:

class Derived : public Base { public : typedef Base super; // note that it could be hidden in // protected/private section, instead // Etc. } ;

这使我能够使用“super”作为Base的别名,例如,在构造函数中:

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

甚至在其覆盖的版本中从基类调用方法时:

void Derived::foo() { super::foo() ; // ... And then, do something else }

它甚至可以链接(我仍然找到的用途,虽然):

class DerivedDerived : public Derived { public : typedef Derived super; // note that it could be hidden in // protected/private section, instead // Etc. } ; void DerivedDerived::bar() { super::bar() ; // will call Derived::bar super::super::bar ; // will call Base::bar // ... And then, do something else }

无论如何,我发现使用“typedef超级”非常有用,例如,当Base是冗长的和/或模板化时。

事实是,超级是在Java中实现的,而在C#中(除非我错了,它被称为“base”)。 但是C ++缺少这个关键字。

所以,我的问题:

这是使用typedef超级常见/罕见/从来没有在你的代码中看到吗? 这是使用typedef超级好吗(即你看到强大还是不强烈的理由不使用它)? 应该“超级”是一件好事,应该在C ++中有些标准化,还是通过一个typedef已经足够了?

编辑: Roddy提到typedef应该是私有的事实。 这将意味着任何派生类将无法使用它而不重新声明它。 但是我想这也会阻止超级超级链接(但谁会为此而哭?)。

编辑2:现在,大量使用“超级”之后的几个月,我全心全意地同意罗迪的观点:“超级”应该是私人的。 我会提醒他的答案两次,但我猜我不能。

My style of coding includes the following idiom:

class Derived : public Base { public : typedef Base super; // note that it could be hidden in // protected/private section, instead // 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 overridden version:

void Derived::foo() { super::foo() ; // ... And then, do something else }

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

class DerivedDerived : public Derived { public : typedef Derived super; // note that it could be hidden in // protected/private section, instead // Etc. } ; void DerivedDerived::bar() { super::bar() ; // will call Derived::bar super::super::bar ; // will call Base::bar // ... And then, do something else }

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

The fact is that super is implemented in Java, as well as in C# (where it is called "base", unless I'm wrong). But C++ lacks this keyword.

So, my questions:

is this use of typedef super common/rare/never seen in the code you work with? is this use of typedef super Ok (i.e. do you see strong or not so strong reasons to not use it)? should "super" be a good thing, should it be somewhat standardized in C++, or is this use through a typedef enough already?

Edit: Roddy mentionned the fact the typedef should be private. This would mean any derived class would not be able to use it without redeclaring it. But I guess it would also prevent the super::super chaining (but who's gonna cry for that?).

Edit 2: Now, some months after massively using "super", I wholeheartedly agree with Roddy's viewpoint: "super" should be private. I would upvote his answer twice, but I guess I can't.

最满意答案

Bjarne Stroustrup在C ++的设计和演进中提到, super作为关键字被ISO C ++标准委员会认为是第一次将C ++标准化。

Dag Bruck提出这种扩展,称为基础类“继承”。 该提案提到了多重继承问题,并将标示着不明确的用途。 即使是Stroustrup也相信。

经过讨论,达格·布鲁克(是的,同一个提出建议的人)写道:该提案是可实施的,技术上合理的,没有重大缺陷,并处理了多重继承。 另一方面,没有足够的轰动,委员会应该处理一个棘手的问题。

迈克尔·蒂曼迟到了,然后表明一个typedefed超级工作很好,使用与这篇文章中提到的相同的技巧。

所以不,这可能永远不会标准化。

如果您没有副本, 设计与演进是非常值得的封面价格。 使用的副本可以约为10美元。

Bjarne Stroustrup mentions in Design and Evolution of C++ that super as a keyword was considered by the ISO C++ Standards committee the first time C++ was standardized.

Dag Bruck proposed this extension, calling the base class "inherited." The proposal mentioned the multiple inheritance issue, and would have flagged ambiguous uses. Even Stroustrup was convinced.

After discussion, Dag Bruck (yes, the same person making the proposal) wrote that the proposal was implementable, technically sound, and free of major flaws, and handled multiple inheritance. On the other hand, there wasn't enough bang for the buck, and the committee should handle a thornier problem.

Michael Tiemann arrived late, and then showed that a typedef'ed super would work just fine, using the same technique that was asked about in this post.

So, no, this will probably never get standardized.

If you don't have a copy, Design and Evolution is well worth the cover price. Used copies can be had for about $10.

更多推荐

本文发布于:2023-08-02 09:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1372727.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:super

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!