• <legend id='kGra1'><style id='kGra1'><dir id='kGra1'><q id='kGra1'></q></dir></style></legend>
    1. <small id='kGra1'></small><noframes id='kGra1'>

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

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

      2. C++ 静态虚拟成员?

        C++ static virtual members?(C++ 静态虚拟成员?)

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

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

                  本文介绍了C++ 静态虚拟成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 C++ 中是否有可能同时具有 staticvirtual 的成员函数?显然,没有一种直接的方法来做到这一点(static virtual member(); 是一个编译错误),但至少有一种方法可以达到同样的效果吗?

                  Is it possible in C++ to have a member function that is both static and virtual? Apparently, there isn't a straightforward way to do it (static virtual member(); is a compile error), but is there at least a way to achieve the same effect?

                  即:

                  struct Object
                  {
                       struct TypeInformation;
                  
                       static virtual const TypeInformation &GetTypeInformation() const;
                  };
                  
                  struct SomeObject : public Object
                  {
                       static virtual const TypeInformation &GetTypeInformation() const;
                  };
                  

                  在实例 (object->GetTypeInformation()) 和类 (SomeObject::GetTypeInformation) 上使用 GetTypeInformation() 是有意义的()),这对比较很有用,对模板很重要.

                  It makes sense to use GetTypeInformation() both on an instance (object->GetTypeInformation()) and on a class (SomeObject::GetTypeInformation()), which can be useful for comparisons and vital for templates.

                  我能想到的唯一方法是为每个类编写两个函数/一个函数和一个常量,或者使用宏.

                  The only ways I can think of involves writing two functions / a function and a constant, per class, or use macros.

                  还有其他解决方案吗?

                  推荐答案

                  不,没有办法,因为当你调用 Object::GetTypeInformation() 时会发生什么?它不知道要调用哪个派生类版本,因为没有与之关联的对象.

                  No, there's no way to do it, since what would happen when you called Object::GetTypeInformation()? It can't know which derived class version to call since there's no object associated with it.

                  您必须使其成为非静态虚拟函数才能正常工作;如果您还希望能够在没有对象实例的情况下以非虚拟方式调用特定派生类的版本,则还必须提供第二个冗余静态非虚拟版本.

                  You'll have to make it a non-static virtual function to work properly; if you also want to be able to call a specific derived class's version non-virtually without an object instance, you'll have to provide a second redunduant static non-virtual version as well.

                  这篇关于C++ 静态虚拟成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)

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

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

                            <tbody id='cBIXX'></tbody>
                        • <tfoot id='cBIXX'></tfoot>

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

                        • <legend id='cBIXX'><style id='cBIXX'><dir id='cBIXX'><q id='cBIXX'></q></dir></style></legend>