简体   繁体   English

具有未命名函数的C ++函数指针数组

[英]C++ Function Pointer Array with unnamed functions

I am attempting to make an array of function pointers. 我试图做一个函数指针数组。

The array is going to be of quite a few functions (between 10 and 50 somewhere). 该数组将具有许多功能(介于10到50之间)。

This declaration is similar to the one I'm using: 此声明类似于我正在使用的声明:

int (*functions[15])(int, int);

The issue I am encountering is that I will have to define all of these functions by hand. 我遇到的问题是我将必须手动定义所有这些功能。 This is fine; 这可以; however I do not care to give a name to all of them. 但是我不在乎给所有人都起个名字。

None of the functions that will be in the array will be called from outside of the array. 数组中的所有函数都不会从数组外部调用。

I know I'm pushing my luck with this question, but is there a way for me to put anonymous functions (similar to delegates, I suppose) into this array? 我知道我对这个问题很幸运,但是有没有办法将匿名函数(类似于代理,我想)放入此数组中?

or could I use a declaration similar to this: 或者我可以使用类似于以下的声明:

int (*functions[15])(int, int);

(*function[0])(int x, int y)
{
    //stuff
}

(*function[1])(int x, int y)
{
    //other stuff
}

I hope to avoid using any C++11 with this, as well, if possible; 我希望尽可能避免与此同时使用任何C ++ 11。 I understand a lot of improvements have been made with it, but I don't know if all compilers would support it yet (I intend on using multiple compilers across platforms). 我知道它已经做了很多改进,但是我不知道所有的编译器是否都将支持它(我打算跨平台使用多个编译器)。

Is there a way to do this without naming every single function, or am I out of luck? 有没有一种方法可以在不命名每个函数的情况下进行操作,还是我不走运?

Without anonymous functions, you need to name your functions. 如果没有匿名函数,则需要命名函数。 There's no alternative in pre C++11. 在C ++ 11之前的版本中没有其他选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM