• <tfoot id='srIy2'></tfoot>

      • <bdo id='srIy2'></bdo><ul id='srIy2'></ul>

      <small id='srIy2'></small><noframes id='srIy2'>

    1. <i id='srIy2'><tr id='srIy2'><dt id='srIy2'><q id='srIy2'><span id='srIy2'><b id='srIy2'><form id='srIy2'><ins id='srIy2'></ins><ul id='srIy2'></ul><sub id='srIy2'></sub></form><legend id='srIy2'></legend><bdo id='srIy2'><pre id='srIy2'><center id='srIy2'></center></pre></bdo></b><th id='srIy2'></th></span></q></dt></tr></i><div id='srIy2'><tfoot id='srIy2'></tfoot><dl id='srIy2'><fieldset id='srIy2'></fieldset></dl></div>

      <legend id='srIy2'><style id='srIy2'><dir id='srIy2'><q id='srIy2'></q></dir></style></legend>

        C++ 风格:为覆盖的方法添加 virtual 关键字前缀

        C++ Style: Prefixing virtual keyword to overridden methods(C++ 风格:为覆盖的方法添加 virtual 关键字前缀)
          1. <legend id='ffgA2'><style id='ffgA2'><dir id='ffgA2'><q id='ffgA2'></q></dir></style></legend>

                <bdo id='ffgA2'></bdo><ul id='ffgA2'></ul>
                <i id='ffgA2'><tr id='ffgA2'><dt id='ffgA2'><q id='ffgA2'><span id='ffgA2'><b id='ffgA2'><form id='ffgA2'><ins id='ffgA2'></ins><ul id='ffgA2'></ul><sub id='ffgA2'></sub></form><legend id='ffgA2'></legend><bdo id='ffgA2'><pre id='ffgA2'><center id='ffgA2'></center></pre></bdo></b><th id='ffgA2'></th></span></q></dt></tr></i><div id='ffgA2'><tfoot id='ffgA2'></tfoot><dl id='ffgA2'><fieldset id='ffgA2'></fieldset></dl></div>

                <small id='ffgA2'></small><noframes id='ffgA2'>

                    <tbody id='ffgA2'></tbody>
                  <tfoot id='ffgA2'></tfoot>
                • 本文介绍了C++ 风格:为覆盖的方法添加 virtual 关键字前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我一直在与我的同事讨论是在重写的方法前添加 virtual 关键字,还是只在原始基类中添加前缀.

                  I've been having a discussion with my coworkers as to whether to prefix overridden methods with the virtual keyword, or only at the originating base class.

                  我倾向于在所有虚方法(即涉及虚表查找的方法)前加上 virtual 关键字.我的理由有三个:

                  I tend to prefix all virtual methods (that is, methods involving a vtable lookup) with the virtual keyword. My rationale is threefold:

                  1. 鉴于 C++ 缺少覆盖关键字,虚拟的存在关键字至少会通知您该方法涉及查找和理论上可以被覆盖进一步的专业化,或者可能是通过指向更高的指针调用基类.

                  1. Given that C++ lacks an override keyword, the presence of the virtual keyword at least notifies you that the method involves a lookup and could theoretically be overridden by further specializations, or could be called through a pointer to a higher base class.

                  一直使用这种风格意味着,当你看到一个方法(至少在我们的代码中)没有virtual关键字,你可以最初假设它既不是派生自基础或专业在子类中.

                  Consistently using this style means that, when you see a method (at least within our code) without the virtual keyword, you can initially assume that it is neither derived from a base nor specialized in subclass.

                  如果由于某些错误,虚拟已从 IFoo 中删除,所有孩子们仍将发挥作用(CFooSpecialization::DoBar 会仍然覆盖 CFooBase::DoBar,而不是简单地隐藏它).

                  If, through some error, the virtual were removed from IFoo, all children will still function (CFooSpecialization::DoBar would still override CFooBase::DoBar, rather than simply hiding it).

                  据我所知,反对这种做法的论点是,但那个方法不是虚拟的"(我认为这是无效的,并且是对虚拟性的误解),以及当我看到 virtual 关键字时,我预计这意味着有人从中得出结论,并去寻找他们."

                  The argument against the practice, as I understood it, was, "But that method isn't virtual" (which I believe is invalid, and borne from a misunderstanding of virtuality), and "When I see the virtual keyword, I expect that means someone is deriving from it, and go searching for them."

                  假设的类可能分布在多个文件中,并且有多个专业化.

                  The hypothetical classes may be spread across several files, and there are several specializations.

                  class IFoo {
                  public:
                      virtual void DoBar() = 0;
                      void DoBaz();
                  };
                  
                  class CFooBase : public IFoo {
                  public:
                      virtual void DoBar(); // Default implementation
                      void DoZap();
                  };
                  
                  
                  class CFooSpecialization : public CFooBase {
                  public:
                      virtual void DoBar(); // Specialized implementation
                  };
                  

                  在风格上,您会从两个派生类中删除 virtual 关键字吗?如果是这样,为什么?Stack Overflow 的想法是什么?

                  Stylistically, would you remove the virtual keyword from the two derived classes? If so, why? What are Stack Overflow's thoughts here?

                  推荐答案

                  我完全同意你的理由.这是一个很好的提醒,该方法在调用时将具有动态调度语义.您同事使用的该方法不是虚拟的"论点完全是虚假的.他混淆了虚拟和纯虚拟的概念.

                  I completely agree with your rationale. It's a good reminder that the method will have dynamic dispatch semantics when called. The "that method isn't virtual" argument that you co-worker is using is completely bogus. He's mixed up the concepts of virtual and pure-virtual.

                  这篇关于C++ 风格:为覆盖的方法添加 virtual 关键字前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to print vector#39;s data(如何打印矢量的数据)
                  Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                  How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                  MSVC++ warning flags(MSVC++ 警告标志)
                  How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                  stack overflow error in C++ program(C++程序中的堆栈溢出错误)

                • <i id='TfKuU'><tr id='TfKuU'><dt id='TfKuU'><q id='TfKuU'><span id='TfKuU'><b id='TfKuU'><form id='TfKuU'><ins id='TfKuU'></ins><ul id='TfKuU'></ul><sub id='TfKuU'></sub></form><legend id='TfKuU'></legend><bdo id='TfKuU'><pre id='TfKuU'><center id='TfKuU'></center></pre></bdo></b><th id='TfKuU'></th></span></q></dt></tr></i><div id='TfKuU'><tfoot id='TfKuU'></tfoot><dl id='TfKuU'><fieldset id='TfKuU'></fieldset></dl></div>
                    <tbody id='TfKuU'></tbody>

                  • <tfoot id='TfKuU'></tfoot>

                    1. <legend id='TfKuU'><style id='TfKuU'><dir id='TfKuU'><q id='TfKuU'></q></dir></style></legend>

                        <bdo id='TfKuU'></bdo><ul id='TfKuU'></ul>

                        <small id='TfKuU'></small><noframes id='TfKuU'>