初始化函数指针数组
简介: 我正在基于 FSM 模式的 VS2010 基本计算器中进行编码。所以,我需要行动图。
如何正确初始化 C++ 中函数指针的静态二维数组? 我已经尝试过
static void (*action_map[])() = {A, pA}; //one dimension for example
,或者
static void (*action_map[])() = {&A, &pA};
其他许多人都不起作用。
添加
一切都应该在课堂上完成。 下面的示例对我不起作用
public class A {
public:
void func1() { cout << "func1\n"; }
void func2() { cout << "func2\n"; }
void func3() { cout << "func3\n"; }
void func4() { cout << "func4\n"; }
typedef void (*function_t)();
function_t function_array[2][2];
A();
};
A::A()
{
function_array[2][2] = { { func1, func2}, { func3, func4 } };
};
int main(array<System::String ^> ^args)
{
A * tst = new A();
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
tst->function_array[i][j]();
}
}
return 0;
}
请指出我到底做错了什么。
Intro:
I'm coding in VS2010 basic calculator based on FSM patter. So, I need action map.
How correctly initialize a static two dimensional array of pointers to functions in C++?
I've already tried
static void (*action_map[])() = {A, pA}; //one dimension for example
or
static void (*action_map[])() = {&A, &pA};
and many others doesn't work.
ADDED
Everything should be done inside class.
Example below doesn't work for me
public class A {
public:
void func1() { cout << "func1\n"; }
void func2() { cout << "func2\n"; }
void func3() { cout << "func3\n"; }
void func4() { cout << "func4\n"; }
typedef void (*function_t)();
function_t function_array[2][2];
A();
};
A::A()
{
function_array[2][2] = { { func1, func2}, { func3, func4 } };
};
int main(array<System::String ^> ^args)
{
A * tst = new A();
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
tst->function_array[i][j]();
}
}
return 0;
}
Please point what exactly I did wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于您使用的是 MSVS2010,它已经实现了许多 C++11 功能,因此可以这样做:
Since you're using MSVS2010 which has implemented many C++11 features, how about doing this:
如果您的编译器支持 C++11 初始化列表,那么您只需删除赋值中的虚假数组大小即可。
或者更好的是,直接初始化它,而不是在默认初始化后分配:
如果您的编译器不支持 C++11,则需要手动分配它们:
您还需要创建函数
static
以便存储指向它们的简单函数指针;如果它们必须是非静态成员,那么您需要存储成员函数指针并使用类实例调用它们,或者存储使用创建的
(或者如果您没有 C++11,则使用它们的 Boost 等效项)。std::function
对象std::bindIf your compiler supports C++11 initialiser lists, then you just need do drop the spurious array sizes in your assignment.
Or better still, initialise it directly, rather than assigning after default-initialisation:
If your compiler doesn't support C++11, you'll need to assign them by hand:
You'll also need to make the functions
static
in order to store simple function pointers to them; if they have to be non-static members, then you'll need to either store member-function pointers and call them with a class instance, or storestd::function
objects, created usingstd::bind
(or their Boost equivalents if you don't have C++11).请注意,类型“function_t”已更改:
如果数组“function_array”在类实例之间可更改,则“static const”不合适,必须在构造函数中填充它。
Note the type 'function_t' has changed:
If the array 'function_array' is changeable between class instances then a 'static const' is not appropriate and it must be populated in the constructor.
如果
A
和pA
是不带参数并返回 void 类型的函数的名称,那么它们都可以。Both of them are fine if
A
andpA
are the name of functions taking no arguments and returning a void type.