使用“超级”在C ++中 [英] Using "super" in C++

查看:110
本文介绍了使用“超级”在C ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  class Derived:public Base 
{
public:
typedef Base super; //注意它可以隐藏在
// protected / private section中,而是

//等等
};

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

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

或者甚至当从覆盖版本中的基类调用方法时: / p>

  void Derived :: foo()
{
super :: foo

// ...然后,执行其他操作
}

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

  class DerivedDerived:public派生的
{
public:
typedef派生的super; //注意它可以隐藏在
// protected / private section中,而是

//等等
};

void DerivedDerived :: bar()
{
super :: bar(); //将调用Derived :: bar
super :: super :: bar; //将调用Base :: bar

// ...然后,执行其他操作
}

无论如何,我发现使用typedef super非常有用,例如,当Base是详细和/或模板时。



事实是super是在Java中实现的,以及在C#(其中它被称为base,除非我错了)。但是C ++缺少这个关键字。



所以,我的问题:




  • 使用typedef超级共同/罕见/从来没有见过在你使用的代码?

  • 这是使用typedef超级Ok(即你看到强或不强烈的理由不使用它)?

  • 应该是超级是好事,是否应该在C ++中标准化,或者是通过typedef足够了吗?



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



      编辑2:几个月后大规模使用超级,我完全同意罗迪的观点:超级应该是私人的。

      解决方案

      Bjarne Stroustrup在设计与进化C ++标准委员会认为 super 作为关键字的第一次C ++标准化。



      Dag Bruck提出了这个扩展,调用了基类inherited。该提案提到了多重继承问题,并且将标记含糊的用途。



      讨论后,Dag Bruck(是的,提出建议的同一个人)写道,该提案是可实施的,技术上完美的,没有重大缺陷,并处理多重继承。另一方面,没有足够的钱来支付赔偿,委员会应该处理一个棘手的问题。



      Michael Tiemann迟到了,然后显示一个typedef



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



      如果您没有副本,设计和演变是非常值得的封面价格。使用的副本可以约$ 10。


      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 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.

      这篇关于使用“超级”在C ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆