void hello(void) { printf("你好!"); }void bye(void) { printf("再见!"); }void ok(void) { printf("好的!"); }typdef void (*funcptr)(void);这样就构造了一个通用的函数你用的时候可以这样:void speak(int id){ funcptr words[3] = {&hello, &bye, &ok}; funcptr fun = words[id]...
定义一个函数指针类型。 比如你有三个函数: void hello(void) { printf("你好!"); } void bye(void) { printf("再见!"); } void ok(void) { printf("好的!"); } typdef void (*funcptr)(void); 这样就构造了一个通用的函数 你用的时候可以这样: void speak(int id) { funcptr words[3] =...
int b) { return a + b; } int main (void) { func_t fp = sum;printf("%d\n", ...
int(*func)(inta,intb); 我们同样可以使用typedef声明一个函数指针类型:func_t typedefint(*func_t)(inta,intb); func_tfp;// 定义一个函数指针变量 写个简单的程序测试一下,运行OK: typedefint(*func_t)(inta,intb); intsum(inta,intb) { returna + b; } intmain(void) { func_tfp = sum; ...
usingInteger =int;usingFuncPtr =void(*)(int,int);usingPointAlias =structPoint; typedef不能用于模板别名,但using可以: template<typenameT>usingVec = std::vector<T>; Vec<int> v;// 相当于 std::vector<int> v; 总之,typedef在C++中是一个有用的工具,可以帮助简化代码和提高可读性,特别是对于复杂...
core.h>usingnamespacestd;voidfunc(void){cout<<"func\n";}typedefvoid(*TFUNC)(void);usingUFUNC=void(*)(void);intmain(intargc,char**argv){//1. 直接定义函数指针void(*fp)(void)=func;fp();//2. typeptr定义一种类型TFUNCfp1=func;fp1();//3. using xx = yyUFUNCuf=func;uf();return...
typedef void (*funcPtr)(int); 上面的语句的意义是:定义了一个函数指针,它可以指向的函数类型是无反回值并且有一个int参数,如下: #include <iostream.h> typedef void (*funcPtr)(int); void print(int a) { cout << a << endl; } void main() ...
#include<stdio.h>typedefint*int_ptr_t;/* const int_ptr_t 被转换成了 int *const, 变成了一个指针常量 */voidfunc(constint_ptr_t cfg){return;}intmain(void){constintdata=10;/* &data 被转换成了 const int*, 是一个指向常量的指针 */func(&data);return0;} ...
void main() { pFun = glFun; (*pFun)(2); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 第一行定义了一个指针变量pFun.它是一个指向某种函数的指针,这种函数参数是一个int类型,返回值是char类型。只有第一句我们还无法使用这个指针,因为我们还未对它进行赋值。
typedef void (*funcptr)(double); //funcptr是一个函数指针类型,指向返回值为void,接受一个double类型参数的函数 typedef union { char c; int i; bool b; } Foo; //Foo是一个联合体 typedef class TestClass { int a, b, c; public: