c ++中的静态虚函数 [英] Static Virtual functions in c++

查看:209
本文介绍了c ++中的静态虚函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基类和一个派生类,我想更改基本函数,同时保持它们是静态的,因为它们应该作为静态传递给其他函数。

I have a base class and a derived one and I want to change base functions while keeping them static as they should be passed to other functions as static.

如何我可以这样做吗?

推荐答案

ATL框架通过使基类成为模板来克服无虚拟静态的限制,然后让派生类将其类类型作为模板参数传递。然后,基类可以在需要时调用派生类静态,例如:

The ATL framework gets around the limitation of no virtual statics by making the base class be a template, and then having derived classes pass their class type as a template parameter. The base class can then call derived class statics when needed, eg:

template< class DerivedType >
class Base
{
public:
  static void DoSomething() { DerivedType::DoSomethingElse(); }
};

class Derived1 : public Base<Derived1>
{
public:
  static void DoSomethingElse() { ... }
};

class Derived2 : public Base<Derived2>
{
public:
  static void DoSomethingElse() { ... }
};

这被称为奇怪的重复模板模式,可用于实现静态多态

This is known as Curiously recurring template pattern, which can be used to implement static polymorphism.

这篇关于c ++中的静态虚函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆