c ++类列表,无需初始化即可使用静态函数 [英] c++ List of classes without initializing them for use of static functions

查看:55
本文介绍了c ++类列表,无需初始化即可使用静态函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能以奇怪的方式问这个问题,但是我不确定还有什么要问的.

I may be asking this in a odd way, but I'm not sure how else to ask.

我想要一个类列表,而不是对象. 这样,我无需创建对象就可以调用静态函数.

I want to have a list of classes, not objects. This way I can call static functions without have to create the object.

推荐答案

在这一点上,我真的很喜欢函数指针:

I would really prefer function pointers at this point:

struct A
{
  void SomeFunc(int);
};

struct B
{
  void AnotherFunc(int);
};


typedef void (*Function)(int);

std::vector<Function> vec;

vec.push_back(A::SomeFunc); vec.push_back(B::AnotherFunc);

for (Function f: vec)
{
  f(2);
}

请注意,静态函数或多或少等同于传统的C函数(它只是需要访问更多范围).

Note that a static function is more or less equivalent to a traditional C-function (it just got to access some more scope).

这篇关于c ++类列表,无需初始化即可使用静态函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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