[英]How can I call a function from an array of functions via its index?
我在网上找不到答案的初学者问题,可能是因为我不知道术语。
我想根据计算的索引值调用程序列表之一。 也就是说,给定一个“1”,调用 firstProc(),“2”调用 secondProc(),依此类推。
所有程序都是没有 arguments 的无效函数。
我可以用 switch/case 来实现它,但我更喜欢的是:
void* action[2] {*firstProc, *secondProc};
(这编译,但警告: invalid conversion from 'void (*)()' to 'void*'
)
然后:
action[get_index()]();
'action' 的编译器对象不能用作 function。
这应该是可能的吧? 我已经尝试了几种变体,但我无法通过使用选定的 ('action[index]') 作为 function。
有两种等效的方法可以做你想做的事。 解释在代码片段中作为注释给出。
#include <iostream>
void foo()
{
std::cout << "Hello";
}
void foo2()
{
std::cout << " wolrd!";
}
int main()
{
void (*a)() = foo;// a is a pointer to a function that takes no parameter and also does not return anything
void (*b)() = foo2;// b is a pointer to a function that takes no parameter and also does not return anything
//create array(of size 2) that can hold pointers to functions that does not return anything and also does not take any parameter
void (*arr[2])() = { a, b};
arr[0](); // calls foo
arr[1](); //calls foo1
return 0;
}
方法一可以在这里执行。
在上面的方法 1 中void (*a)() = foo;
表示a
是一个指向 function 的指针,它不带任何参数,也不返回任何内容。
同样, void (*b)() = foo2;
表示b
是一个指向 function 的指针,它不带参数,也不返回任何内容。
接下来, void (*arr[2])() = { a, b};
意味着arr
是一个数组(大小为 2),它可以保存指向不返回任何内容且不带任何参数的函数的指针。
#include <iostream>
void foo()
{
std::cout << "Hello";
}
void foo2()
{
std::cout << " wolrd!";
}
int main()
{
//create array(of size 2) that can hold pointers to functions that does not return anything
void (*arr[2])() = { foo, foo2};
arr[0](); // calls foo
arr[1](); //calls foo1
return 0;
}
方法二可以在这里执行。
我已经停止使用 function 指针(尽管它们仍然有用)。 在使用函数时,我通常使用 std::function (和 lambdas)
arrays 的函数代码如下所示。 我使用了 std::vector 但 std::array 固定大小也应该可以正常工作。
#include <vector>
#include <functional>
#include <iostream>
void some_function()
{
std::cout << "some function\n";
}
int main()
{
// std::function, abstraction of a function, function signature = template parameter, so void () is function returning a void, no parameters
// std::vector, runtime resizable array
// constructor : 4 time a lambda function printing out hello world.
std::vector<std::function<void()>> functions(4, [] { std::cout << "Hello World!\n"; } );
// easy syntax to assign an existing function to an index
functions[1] = some_function;
// replace a function in the vector with another one (lambda)
functions[2] = [] { std::cout << "booh\n"; };
// call function at index 0
functions[0]();
std::cout << "\n\n";
// or loop over all the functions and call them (classic for loop)
for (std::size_t n = 0; n < functions.size(); ++n) functions[n]();
std::cout << "\n\n";
// or loop over all the functions (range based for loop)
for (const auto& function : functions) function();
return 0;
}
您的 function 指针数组需要正确的语法。 void(*func_ptr[])()
。
例子:
void func1() { std::cout << "Hallo "; }
void func2() { std::cout << "World";}
int main()
{
void(*func_ptr[])()={ func1, func2 };
for ( unsigned int idx = 0; idx<2; idx++ )
{
func_ptr[idx]();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.