<legend id='0w0m9'><style id='0w0m9'><dir id='0w0m9'><q id='0w0m9'></q></dir></style></legend>
          <bdo id='0w0m9'></bdo><ul id='0w0m9'></ul>

        <small id='0w0m9'></small><noframes id='0w0m9'>

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

      1. 派生类如何从基类继承静态函数?

        How can Derived class inherit a static function from Base class?(派生类如何从基类继承静态函数?)

        <small id='0CG0q'></small><noframes id='0CG0q'>

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

                1. 本文介绍了派生类如何从基类继承静态函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  struct TimerEvent
                  {
                     event Event;
                     timeval TimeOut;
                     static void HandleTimer(int Fd, short Event, void *Arg);
                  };
                  

                  HandleTimer 需要是静态的,因为我将它传递给 C 库 (libevent).

                  HandleTimer needs to be static since I'm passing it to C library (libevent).

                  我想继承这个类.这怎么办?

                  I want to inherit from this class. How can this be done?

                  谢谢.

                  推荐答案

                  您可以轻松继承该类:

                  class Derived: public TimerEvent {
                      ...
                  };
                  

                  但是,您不能在子类中覆盖 HandleTimer 并期望它起作用:

                  However, you can't override HandleTimer in your subclass and expect this to work:

                  TimerEvent *e = new Derived();
                  e->HandleTimer();
                  

                  这是因为静态方法在 vtable 中没有条目,因此不能是虚拟的.但是,您可以使用void* Arg"将指针传递给您的实例……例如:

                  This is because static methods don't have an entry in the vtable, and can't thus be virtual. You can however use the "void* Arg" to pass a pointer to your instance... something like:

                  struct TimerEvent {
                      virtual void handle(int fd, short event) = 0;
                  
                      static void HandleTimer(int fd, short event, void *arg) {
                          ((TimerEvent *) arg)->handle(fd, event);
                      }
                  };
                  
                  class Derived: public TimerEvent {
                      virtual void handle(int fd, short event) {
                          // whatever
                      }
                  };
                  

                  这样,HandleTimer 仍然可以在 C 函数中使用,只需确保始终将真实"对象作为void* Arg"传递.

                  This way, HandleTimer can still be used from C functions, just make sure to always pass the "real" object as the "void* Arg".

                  这篇关于派生类如何从基类继承静态函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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++ 引用发生变化)

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

                        • <small id='WxSDl'></small><noframes id='WxSDl'>