I have got 2 functions:
我有两个函数:
char* odwroc(char* nap, int n)
char* male(char* nap, int n)
I have defined a pointer to that kind functions
我已经定义了一个指向那种函数的指针。
typedef char*(*pointerToFunction )( char*, int );
then in used that definition in main:
然后在主要的定义中:
pointerToFunction ptr1 = odwroc;
pointerToFunction ptr2 = male;
but now I have to create a function which as a first parameter gets array of that pointers to function and I am stuck. I don't know how to define array of pointers to function and how the modyfikuj
parameter list should look like.
但现在我要创建一个函数,作为第一个参数,它会得到一个指向函数的数组,我被卡住了。我不知道如何定义指向函数的指针数组,以及modyfikuj参数列表应该是什么样的。
void modyfikuj(char* pointerToFunction *pointerArray, int length, char* nap2, int n){
}
2 个解决方案
#1
1
Try this:
试试这个:
pointerToFunction mojefunkcje[] = { odwroc, male};
modyfikuj( mojefunkcje, ...); // pass the array fo modyfikuj()
void modyfikuj( pointerToFunction* funtab, ...)
{
funtab[0]( string, liczba); // call odwroc( string, liczba)
funtab[1]( string, liczba); // call male( string, liczba)
}
#2
1
Even though the above answer make sense, use of containers such as std::vector will give you more control when passing an array of similar type such as a pointer to a function. Please try below code snippet.
尽管上面的答案是合理的,但是使用std::vector这样的容器将会在传递类似于函数指针的数组时给您更多的控制。请尝试下面的代码片段。
#include "vector"
using namespace std;
typedef char*(*pointerToFunction )( char*, int );
typedef vector<pointerToFunction> FUNCTION_VECTOR;
bool modyfikuj( FUNCTION_VECTOR& vecFunctionVector )
{
// The below checking ensures the vector does contain at least one function pointer to be called.
if( vecFunctionVector.size() <= 0 )
{
return false;
}
// You can have any number of function pointers to be passed and get it executed, one by one.
FUNCTION_VECTOR::iterator itrFunction = vecFunctionVector.begin();
FUNCTION_VECTOR::const_iterator itrFunEnd = vecFunctionVector.end();
char* cszResult = 0;
for( ; itrFunEnd != itrFunction; ++itrFunction )
{
cszResult = 0;
// Here goes the function call!
cszResult = (*itrFunEnd)( "Hello", 1 );
// Check cszResult for any result.
}
return true;
}
char* odwroc(char* nap, int n); // You will define this function somewhere else.
char* male(char* nap, int n); // You will define this function somewhere else.
int main()
{
FUNCTION_VECTOR vecFunctions;
// You can push as many function pointers as you wish.
vecFunctions.push_back( odwroc );
vecFunctions.push_back( male );
modyfikuj( vecFunctions );
return 0;
}
#1
1
Try this:
试试这个:
pointerToFunction mojefunkcje[] = { odwroc, male};
modyfikuj( mojefunkcje, ...); // pass the array fo modyfikuj()
void modyfikuj( pointerToFunction* funtab, ...)
{
funtab[0]( string, liczba); // call odwroc( string, liczba)
funtab[1]( string, liczba); // call male( string, liczba)
}
#2
1
Even though the above answer make sense, use of containers such as std::vector will give you more control when passing an array of similar type such as a pointer to a function. Please try below code snippet.
尽管上面的答案是合理的,但是使用std::vector这样的容器将会在传递类似于函数指针的数组时给您更多的控制。请尝试下面的代码片段。
#include "vector"
using namespace std;
typedef char*(*pointerToFunction )( char*, int );
typedef vector<pointerToFunction> FUNCTION_VECTOR;
bool modyfikuj( FUNCTION_VECTOR& vecFunctionVector )
{
// The below checking ensures the vector does contain at least one function pointer to be called.
if( vecFunctionVector.size() <= 0 )
{
return false;
}
// You can have any number of function pointers to be passed and get it executed, one by one.
FUNCTION_VECTOR::iterator itrFunction = vecFunctionVector.begin();
FUNCTION_VECTOR::const_iterator itrFunEnd = vecFunctionVector.end();
char* cszResult = 0;
for( ; itrFunEnd != itrFunction; ++itrFunction )
{
cszResult = 0;
// Here goes the function call!
cszResult = (*itrFunEnd)( "Hello", 1 );
// Check cszResult for any result.
}
return true;
}
char* odwroc(char* nap, int n); // You will define this function somewhere else.
char* male(char* nap, int n); // You will define this function somewhere else.
int main()
{
FUNCTION_VECTOR vecFunctions;
// You can push as many function pointers as you wish.
vecFunctions.push_back( odwroc );
vecFunctions.push_back( male );
modyfikuj( vecFunctions );
return 0;
}