Bis*_*tev 15 c++ inheritance static
我在覆盖基本类的静态方法方面遇到了一些问题,但整个问题非常复杂且太长(游戏引擎中资源管理的普遍化),所以这里有一个简化版本:
template<class T>
class base
{
static void bar()
{ printf("bar"); }
public:
static void foo()
{ bar(); }
};
class derived : public base<int>
{
static void bar()
{ printf("baz"); }
};
int main() { derived::foo(); }
Run Code Online (Sandbox Code Playgroud)
上面的代码在我的情况下输出"bar",我希望它输出"baz".我该怎么办呢?似乎无论我尝试什么,base :: foo()总是调用base :: bar().我的设计可能有问题.我从未遇到过这个问题 - 我该如何解决?
YSC*_*YSC 30
你想要做的事情是通过简单的类继承无法实现的; 一个方法不能兼得static和virtual.
你需要一个static能够在没有对象(实例)的情况下调用函数的方法; 你需要bar为virtual使bar<int>::foo()呼叫derived::bar()从一个调用时derived 实例.
这两个特征是相互排斥的.但奇怪的递归模板模式(CRTP)可能是一个解决方案:
#include <iostream>
template<class T>
struct base
{
static void foo()
{
T::bar();
}
};
struct derived : public base<derived>
{
static void bar()
{
std::cout << "derived" << std::endl;
}
};
int main()
{
derived::foo();
}
Run Code Online (Sandbox Code Playgroud)