C++ 派生类如何从基类继承静态函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/515763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:44:42  来源:igfitidea点击:

How can Derived class inherit a static function from Base class?

c++inheritancestatic

提问by Hymanhab

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

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

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

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

我想从这个类继承。如何才能做到这一点?

Thanks.

谢谢。

回答by Joao da Silva

You can easily inherit from that class:

您可以轻松地从该类继承:

class Derived: public TimerEvent {
    ...
};

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

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

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

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:

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

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
    }
};

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

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

回答by xavlours

To some extent the traits pattern lets you inherit and redefine static methods.

在某种程度上,traits 模式允许您继承和重新定义静态方法。

First start with a base class:

首先从一个基类开始:

struct base {
  static void talk()  { std::cout << "hello" << std::endl; }
  static void shout() { std::cout << "HELLO !!" << std::endl; }
};

Then derive it and redefine some methods:

然后派生出来,重新定义一些方法:

struct derived: public base {
  static void talk()  { std::cout << "goodbye" << std::endl; }
};

And now call the methods via a traits class:

现在通过一个traits类调用这些方法:

template < class T >
struct talker_traits {
  static void talk() { T::talk(); }
  static void shout() { T::shout(); }
};

talker_traits<base>::talk()     // prints "hello"
talker_traits<base>::shout()    // prints "HELLO !!"

talker_traits<derived>::talk()  // prints "goodbye"
talker_traits<derived>::shout() // prints "HELLO !!"

ideone demo

ideone 演示

The traits class lets you reuse the static method base::shoutwhile "overriding" base::talkwith derived::talk. Still, there are several difference with actual inheritance:

traits类可以重复使用的静态方法base::shout,而“覆盖”base::talkderived::talk。尽管如此,与实际继承还是有一些区别:

  • The function to call is resolved at compile time
  • The child method needs not have the same signature as the parent one
  • 要调用的函数在编译时解析
  • 子方法不需要与父方法具有相同的签名

It works with static fields and typedefs too, the best example is std::iterator_traits.

它也适用于静态字段和 typedef,最好的例子是std::iterator_traits

回答by MSalters

You've got a bit of a conflict here in your question. When you pass &TimerEvent::TimerHandlerto a C library, you do exactly that. You could also have passed &DerivedTimerEvent::TimerHandler, if you wanted. But you can't pass &TimerEvent::TimerHandlerand expect the C library(!) to figure out you actually meant &DerivedTimerEvent::TimerHandler.

你的问题在这里有点冲突。当您传递&TimerEvent::TimerHandler给 C 库时,您就是这样做的。&DerivedTimerEvent::TimerHandler如果你愿意,你也可以通过。但是您不能通过&TimerEvent::TimerHandler并期望C 库(!)弄清楚您实际上是指&DerivedTimerEvent::TimerHandler.