C++学习笔记之 函数重载和函数指针在一起

来源:互联网 发布:自动化设备 图形 编程 编辑:程序博客网 时间:2024/06/06 15:02

笔记:

当使用重载函数名对函数指针进行赋值时,根据重载规则挑选与指针参数列表一致的候选者。严格匹配候选者的函数类型与函数指针的函数类型。

int func(int x){return x;}int func(int a,int b){return a + b;}int func(const char* s){return strlen(s);}typedef int(*PFUNC)(int a);//int(int a);int main(){int c = 0;PFUNC p = func;c = p(1);cout << c << endl;return 0;}


void myfunc(int a,int b){printf("a:%d,b:%d",a,b);}void myfunc(double a, double b){printf("a:%f,b:%f", a, b);}//函数指针 基础语法//声明一个函数类型//void myfunc(int a,int b);typedef void(myTypeFunc)(int a, int b);//myTypeFunc *myfuncp=NULL;定义一个函数指针,这个指针指向函数的入口地址。//声明一个函数指针类型typedef void(*myPTypeFunc)(int a,int b);//声明了一个指针的数据类型//myPTypeFunc fp=NULL;通过 函数指针类型 定义了一个函数指针//定义一个函数指针变量void(*myVarPFunc)(int a, int b);int main(){myPTypeFunc fp;//定义了一个函数指针变量fp = myfunc;fp(1,2);return 0;}



0 0