我在網上找不到答案的初學者問題,可能是因為我不知道術語。
我想根據計算出的索引值呼叫一系列程序之一。也就是說,給定一個 '1',呼叫 firstProc(),'2' 呼叫 secondProc() 等等。
所有的程序都是沒有引數的空函式。
我可以用 switch/case 來實作,但我更喜歡的是:
void* action[2] {*firstProc, *secondProc};
(這將編譯,但警告說:invalid conversion from 'void (*)()' to 'void*')
然后:
action[get_index()]();
'action' 不能用作函式的編譯器物件。
這應該是可能的吧?我已經嘗試了幾種變體,但我無法通過使用選定的 ('action[index]') 作為函式。
uj5u.com熱心網友回復:
有兩種等效的方法可以做你想做的事。解釋作為代碼片段中的注釋給出。
方法一
#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可以在這里執行。
另外,在上述方法1void (*a)() = foo; 的手段即a是一個指向不帶任何引數,并且還不會回傳任何一個功能。
同樣,void (*b)() = foo2; 手段即b是一個指向不帶引數的函式,也不會回傳任何東西。
接下來,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;
}
方法2可以在這里執行。
uj5u.com熱心網友回復:
您的函式指標陣列需要正確的語法。void(*func_ptr[])().
例子:
void func1() { std::cout << "Hallo" << std::endl; }
void func2() { std::cout << "World" << std::endl; }
// if you need a different signature for your functions like:
int func3(int n) { std::cout << "n1 " << n << std::endl; return n*2; }
int func4(int n) { std::cout << "n2 " << n << std::endl; return n*3; }
int main()
{
// array of function pointer which
// have no parameter and void as return value
void(*func_ptr[])()={ func1, func2 };
for ( unsigned int idx = 0; idx<2; idx )
{
func_ptr[idx]();
}
// array of function pointers with int return value and int as
// parameter
int(*func_ptr2[])(int)={ func3, func4 };
for ( unsigned int idx = 0; idx<2; idx )
{
std::cout << "retval: " << func_ptr2[idx](6) << std::endl;
}
}
uj5u.com熱心網友回復:
我已經停止使用函式指標(盡管它們仍然有用)。在處理函式時,我通常使用 std::function(和 lambdas)
函式陣列的代碼如下所示。我使用 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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/382263.html
