【发布时间】:2011-08-03 08:31:49
【问题描述】:
例如:
struct Foo
{
int bar;
int (*baz)(int);
};
int testFunc(int x)
{
return x;
}
Foo list[] = {
{ 0, &testFunc },
{ 1, 0 } // no func for this.
};
在本例中,我宁愿将函数直接放入 list[] 初始化程序,而不是使用指向在别处声明的函数的指针;它将相关的代码/数据保存在同一个地方。
有没有办法做到这一点?我尝试了所有我能想到的语法,但无法让它发挥作用。
【问题讨论】:
-
这看起来非常接近动态调度的手动实现......你考虑过使用继承吗?
标签: c++ struct function-pointers