[英]How Can I Assign a Function to a Function Pointer
I would like to be able to do something like this: 我希望能够做这样的事情:
void test();
void (*testPointer)() = SomethingThatReturnsAFunctionPointer();
test = testPointer;
I would like to make something that functions similarly to the way openGL headers are implemented in which function prototypes are declared, and then the functions are set to a pointer. 我想做一些功能类似于实现openGL标头的方法,在该方法中声明函数原型,然后将函数设置为指针。 In other words, I would like to know how some openGL header files are able to both load the openGL functions, and have prototypes of the functions at the same time.
换句话说,我想知道一些openGL头文件如何既可以加载openGL函数,又可以同时具有这些函数的原型。
Functions are not variables; 函数不是变量; they cannot be assigned to.
他们不能被分配给。
However, functions can be implicitly converted to pointers to those functions. 但是,可以将函数隐式转换为指向这些函数的指针。 The function itself still isn't a variable, but you can assign that function pointer to a variable that is suitably typed for that function pointer.
函数本身仍然不是变量,但是您可以将该函数指针分配给为该函数指针适当键入的变量。
I would like to know how some openGL header files are able to both load the openGL functions, and have prototypes of the functions at the same time.
我想知道一些openGL头文件如何既可以加载openGL函数,又可以同时具有这些函数的原型。
I don't know which particular header you're talking about, but the loader I created simply has the implementation of the function call the function pointer, passing it all of the parameters and returning its value (if any). 我不知道您要说的是哪个特定的标头,但是我创建的加载器只是实现了函数调用函数指针的实现,将所有参数传递给它并返回其值(如果有)。 The pointer is defined inside a source file, so it's not in the header itself.
指针是在源文件中定义的,因此它不在标题本身中。
Using your example: 使用您的示例:
//header
void test();
//source file
void (*testPointer)();
void test()
{
testPointer();
}
You can even get fancy and make test
load the pointer : 您甚至可以幻想并
test
加载指针 :
//source file
void (*testPointer)() == NULL;
void test()
{
if(!testPointer)
{
testPointer = SomethingThatReturnsAFunctionPointer();
}
testPointer();
}
1st: 第一:
int aplusb(int a, int b){return a + b;}
int aminusb(int a, int b){return a - b;}
int (*func)(int a, int b) = aplusb;
int some_func_caller ( int A, int B, int (*func)(int a, int b)){
return func(A, B);
}
int main(){
int a_ =10, b_ = 7;
int res1 = func(a_, b_);
int res2 = somefunc_caller(a_, b_, aminusb);
return 0;
}
2nd: 第二名:
(if you re using c++ compiler) (如果您使用的是c ++编译器)
typedef int MyFuncionType(int a, int b);
typedef int (*MyFuncionType2)(int a, int b);
int aplusb(int a, int b){return a + b;}
int aminusb(int a, int b){return a - b;}
int some_function_caller1(int a, int b, MyfunctionType fc){return fc(a,b);}
int some_function_caller2(int a, int b, MyfunctionType2 fc){return fc(a,b);}
int main(){
int a_ = 10, b_ = 7;
MyFunctionType *func1 = aminusb, *func2 = nullptr;
MyFunctionType2 func3 = aplusb, func4 = nullptr;
int res1 = func1(a_, b_);
int res2 = func3(a_, b_);
int res3 = some_function_caller1(a_, b_, aplusb);
int res4 = some_function_caller2(a_, b_, aminusb);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.