传智扫地僧课程学习笔记。
先梳理下函数指针的基础语法,
1,
void myfunc( int a, int b);
typedef void (myTypeFunc)(int a, int b);
myTypeFunc *myfuncp = NULL;
2,
void myfunc( int a, int b);
typedef void (*myPTypeFunc)(int a, int b);
myTypeFunc myfuncp = NULL;
//注意与1的区别,是*
3,
void (*myVarPFunc)( int a, int b);
然后就开始说了,
函数重载和函数指针在一起的时候,会怎么样呢?
会不会因为在函数调用的时候,根据不同的参数,就调用不同的函数呢?
其实是不会的,
因为函数指针自己本身就有确定的类型,
在调用的时候会执行严格的类型检查,
void myfunc( double a, double b)
{
cout<<"double kill"<<endl;
cout<<a<<endl<<b<<endl;
}
typedef void (*mypfunc)( int a, int b);
int main()
{
mypfunc p = NULL;
p = myfunc;
p( 1.0, 2.0);
system("pause");
return 0;
}
这里会报错的哦!
ps:
typedef int(*PFUNC)(int a);
PFUNC p = func;
因为typedef的存在,PFUNC成为了一种类型,