初始化函数指针数组

发布于 2024-12-15 12:36:53 字数 1031 浏览 0 评论 0原文

简介: 我正在基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

流心雨 2024-12-22 12:36:55

由于您使用的是 MSVS2010,它已经实现了许多 C++11 功能,因此可以这样做:

void f1() {}
void f2() {}
void f3() {}
void f4() {}

std::vector<std::function<void()>> action_map = {f1, f2, f3, f4};

for(size_t i = 0 ; i < action_map.size(); ++i)
{
     action_map[i](); //invoke action!
}

Since you're using MSVS2010 which has implemented many C++11 features, how about doing this:

void f1() {}
void f2() {}
void f3() {}
void f4() {}

std::vector<std::function<void()>> action_map = {f1, f2, f3, f4};

for(size_t i = 0 ; i < action_map.size(); ++i)
{
     action_map[i](); //invoke action!
}
爱她像谁 2024-12-22 12:36:54

如果您的编译器支持 C++11 初始化列表,那么您只需删除赋值中的虚假数组大小即可。

A::A() 
{
    function_array = { { func1, func2}, { func3, func4 } };
}

或者更好的是,直接初始化它,而不是在默认初始化后分配:

A::A() : function_array { { func1, func2}, { func3, func4 } }
{}

如果您的编译器不支持 C++11,则需要手动分配它们:

A::A()
{
    function_array[0][0] = func1;
    function_array[0][1] = func2;
    function_array[1][0] = func3;
    function_array[1][1] = func4;
}

您还需要创建函数 static 以便存储指向它们的简单函数指针;如果它们必须是非静态成员,那么您需要存储成员函数指针并使用类实例调用它们,或者存储使用 创建的 std::function 对象std::bind (或者如果您没有 C++11,则使用它们的 Boost 等效项)。

If your compiler supports C++11 initialiser lists, then you just need do drop the spurious array sizes in your assignment.

A::A() 
{
    function_array = { { func1, func2}, { func3, func4 } };
}

Or better still, initialise it directly, rather than assigning after default-initialisation:

A::A() : function_array { { func1, func2}, { func3, func4 } }
{}

If your compiler doesn't support C++11, you'll need to assign them by hand:

A::A()
{
    function_array[0][0] = func1;
    function_array[0][1] = func2;
    function_array[1][0] = func3;
    function_array[1][1] = func4;
}

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 store std::function objects, created using std::bind (or their Boost equivalents if you don't have C++11).

灯角 2024-12-22 12:36:54

请注意,类型“function_t”已更改:

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 (A::*function_t)();
    static const function_t function_array[2][2];
};

const A::function_t A::function_array[2][2] = { { &A::func1, &A::func2 },
                                                { &A::func3, &A::func4 }
                                              };

// Example use.
A my_a;
for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 2; j++)
    {
        std::mem_fn(A::function_array[i][j])(my_a);
    }
} 

如果数组“function_array”在类实例之间可更改,则“static const”不合适,必须在构造函数中填充它。

Note the type 'function_t' has changed:

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 (A::*function_t)();
    static const function_t function_array[2][2];
};

const A::function_t A::function_array[2][2] = { { &A::func1, &A::func2 },
                                                { &A::func3, &A::func4 }
                                              };

// Example use.
A my_a;
for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 2; j++)
    {
        std::mem_fn(A::function_array[i][j])(my_a);
    }
} 

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.

じ违心 2024-12-22 12:36:54

如果 ApA 是不带参数并返回 void 类型的函数的名称,那么它们都可以。

Both of them are fine if A and pA are the name of functions taking no arguments and returning a void type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文