#include<iostream>
using namespace std;
//重载函数的候选者
void myfunc(int a)
{
cout << "a = " << a << endl;
}
void myfunc(char *p)
{
scanf("p: %s\n", p);
}
void myfunc(int a, int b)
{
cout << "a = " << a << " b = " << b << endl;
}
void myfunc(char *p1, char *p2)
{
printf("p1 = %s\np2 = %s\n", p1, p2);
}
//函数指针
//1.声明一个函数类型
typedef void (myTypeFunc)(int a, int b);
//myTypeFunc *fp = NULL; 定义一个函数指针 指向函数入口地址
//2.声明一个函数指针类型
typedef void (*myPTypeFunc)(int a, int b); //声明了一个指针的类型
//myPTypeFunc fp = NULL; 通过函数指针类型 定义了一个函数指针
//3.定义一个函数指针 变量
void (*myVarPFunc)(int a, int b);
int main()
{
//定义了一个 函数指针 变量
myPTypeFunc fp;
fp = myfunc;
fp(1, 2);
//严格匹配候选者的函数类型与函数指针的函数类型
//不匹配时会报错
//2. 的函数指针的函数类型是 int 型 候选者为 char 型
/*
{
char buf1[] = "aaaa";
char buf2[] = "bbbb";
fp(buf1, buf2);
}
*/
return 0;
}
函数重载与函数指针详解
最新推荐文章于 2024-08-06 16:24:59 发布