简体   繁体   English

如何使用指向函数的指针数组?

[英]How to use an array of pointers to functions?

I want to use for example this array of pointers to functions, without using STL. 例如,我想使用指向函数的指针数组,而不使用STL。

That array is an array of pointers that I call functions OptionA , OptionB and so on. 该数组是一个指针数组,我将其称为函数OptionAOptionB等。

int(*Functions[4])();
Functions[0] = OpionA;
Functions[1] = OptionB;
Functions[2] = OptionC;
Functions[0] = Exit;

Now if I write inside the function where I have my array 现在,如果我在有数组的函数中编写代码

Functions[0];

I want to have called function 'OptionA' where it has been defined before for example like this: 我想在上面已经定义过的地方调用函数“ OptionA”,例如:

int OptionA()
{
    cout << "OPTION A";
    _getch();
    return 0;
}

Is it possible to do this without STL? 没有STL,是否可以这样做? If not, I would like to know how to do it with STL. 如果没有,我想知道如何使用STL。

You can create and pass arrays of function pointers like any other types. 您可以像创建其他类型一样创建和传递函数指针数组。 It's easiest if you have a type alias (my example leverages using , but typedef will also work). 如果您有类型别名,这是最简单的方法(我的示例using ,但typedef也可以使用)。

#include <iostream>

using Function = int (*)(int, int);

int add(int a, int b) {
    return a + b;
}

int sub(int a, int b) {
    return a - b;
}

void do_stuff(int a, int b, Function * fns, int cnt) {
    for(auto i = 0; i < cnt; ++i) {
        std::cout << "Result " << i << " = " << fns[i](a, b) << '\n';
    }
}

int main() {
    Function fns[2] = { add, sub };
    do_stuff(10, 7, fns, 2);
    return 0;
}

Output: 输出:

Result 0 = 17
Result 1 = 3

I think that what you are looking for is 我认为您正在寻找的是
How to initialize a vector of pointers 如何初始化指针向量

Once your vector is initialize you can send it to a function like a normal data type. 一旦初始化了向量,就可以将其发送给类似于普通数据类型的函数。 Example: 例:

std::vector<int*> array_of_pointers{ new int(0), new int(1), new int(17) };
function(array_of_pointers);

In the declaration of the function 在函数的声明中

void function(std::vector<int*> array_of_pointers);

I hope this answer your question. 我希望这能回答您的问题。

In C and C++, arrays are second-class. 在C和C ++中,数组是第二类的。 They cannot be passed by value by themselves, only if somehow wrapped. 只有通过某种方式包装它们,它们自己才能通过价值传递。

As a first step, the questions you have to decide are: 第一步,您需要确定的问题是:

  1. Does your array have a fixed length? 您的数组长度是否固定?
  2. And do you have to pass it by value or can you pass it by reference ? 并且您必须按值传递它还是可以按引用传递它?

If you have to pass it by value, is that a choice you want the caller to make, or the callee to impose? 如果您必须按值传递它,那是您希望呼叫者做出的选择,还是被呼叫者施加的选择? In the first case, pass it by reference. 在第一种情况下,请通过引用传递它。

If you pass the array by reference, nothing can beat using a gsl::span , unless you pass multiple sequences all having intrinsically the same length, in which case passing pointers and a single length-argument is more efficient and maybe comfortable. 如果通过引用传递数组,则使用gsl::span ,除非传递多个序列,这些序列本质上具有相同的长度,在这种情况下,传递指针和单个长度参数会更有效,也可能更舒适。

If you pass an array of variable length by value, try to use a std::vector . 如果按值传递可变长度的数组,请尝试使用std::vector That's also the go-to type to pass a by-ref argument as if by-value. 这也是传递by-ref参数(就像by-value一样)的首选类型。

Otherwise (array of fixed length, by value), nothing beats std::array . 否则(按值固定长度的数组),没有什么比std::array

If p is a pointer to a function, which receives no parameters, you should call it by this syntax: 如果p是指向不接收任何参数的函数的指针,则应使用以下语法调用它:

p();

So, if array is an array of pointers to functions, you should call one of them using the same syntax idea: 因此,如果array是指向函数的指针的数组,则应使用相同的语法来调用其中一个:

array[0]();

Here the parentheses are important; 这里的括号很重要; they say "call this function, and pass no parameters to it". 他们说“调用此函数,并且不传递任何参数”。 If you have no parentheses 如果没有括号

array[0];

this means "select this function from the array, but do nothing with it". 这意味着“从数组中选择此函数,但不执行任何操作”。

It's a useless expression, like if you have an integer x , then x * 5 means "multiply x by 5 and do nothing with the result" (useless), while x *= 5 means "multiply x by 5 and replace x with the result". 这是一个无用的表达式,例如,如果您有一个整数x ,则x * 5表示“将x乘以5并对结果不做任何事”(无用),而x *= 5表示“将x乘以5并将x替换为结果”。

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

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