第4课 - 函数的升级-下
1. 重载的概念
同一个标示符在不同的上下文有不同的意义。
2. 函数的重载
用同一个函数名定义不同的函数,当函数名和不同的参数搭配的时候函数的含义不同。
int func(int x) //一个int参数
{
return x;
}
int func(int a, int b) //两个int参数
{
return a + b;
}
int func(const char* s) //一个char*参数
{
return strlen(s);
}
函数重载至少满足下面的一个条件:参数个数不同,参数类型不同,参数顺序不同。
#include <stdio.h>
#include <string.h>
int func(int x)
{
return x;
}
int func(int a, int b)
{
return a + b;
}
int func(const char* s)
{
return strlen(s);
}
int func(int a, const char* s)
{
return a;
}
int func(const char* s, int a)
{
return strlen(s);
}
int main(int argc, char *argv[])
{
int c = 0;
c = func("ab", 1); //不知道是该调用哪个函数。
printf("c = %d\n", c);
printf("Press enter to continue ...");
getchar();
return 0;
}
l 当函数默认参数遇到函数重载的时候:
#include <stdio.h>
#include <string.h>
int func(int a, int b, int c = 0)
{
return a * b * c;
}
int func(int a, int b)
{
return a + b;
}
int main(int argc, char *argv[])
{
int c = 0;
c = func(1, 2); // 存在二义性,调用失败,编译不能通过
printf("c = %d\n", c);
printf("Press enter to continue ...");
getchar();
return 0;
}
l 编译器调用重载函数的准则:
将所有的同名函数作为候选者,尝试寻找可行的候选函数:精确匹配实参,通过默认参数能够匹配实参,通过默认类型转换匹配实参。
匹配失败:最终寻找到的可行候选函数不是唯一的,就会出现二义性,编译失败。无法匹配所有的候选者,函数没有定义,编译失败。
l 函数重载的注意事项:
重载函数在本质上是相互独立的不同的函数,重载函数的函数类型是不同的,函数返回值不能作为函数重载的依据。函数重载是由函数名和参数列表决定的。
#include <stdio.h>
#include <string.h>
int func(int x) // int(int a)
{
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 argc, char *argv[])
{
int c = 0;
PFUNC p = func;
c = p(1);
printf("c = %d\n", c);
printf("Press enter to continue ...");
getchar();
return 0;
}
运行结果:c = 1
l 函数重载与函数指针
当使用重载函数名对函数指针进行赋值时,根据重载规则挑选与函数指针参数列表一致的候选者,严格匹配候选者的函数类型与函数指针的函数类型。
3. C++和C的相互调用
在项目中融合C++和C代码是实际工程中不可避免的。虽然C++编译器能够兼容C语言的编译方式,但是C++编译器会优先使用C++方式进行编译。利用extern关键字强制让C++编译器对代码进行C方式编译。
C++调用C编写的函数:
add.h
int add(int a, int b);
add.c
#include "add.h"
int add(int a, int b)
{ return a + b; }
main.c
#include <stdio.h>
extern "C"
{
#include "add.h"
}
int main()
{
printf("1 + 2 = %d\n", add(1, 2));
return 0;
}
C调用C++编写的函数
add.h
int add(int a, int b);
add.c
extern "C"
{
#include "add.h"
int add(int a, int b)
{
return a + b;
}
}
main.c
#include <stdio.h>
#include "add.h"
int main()
{
printf("1 + 2 = %d\n", add(1, 2));
return 0;
}
统一的解决方案
_cplusplus是C++编译器内置的标准宏定义。
_cplusplus的意义:让C代码可以通过C编译器的编译,也可以在C++编译器中以C的方式编译。
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
l 注意:C++编译器中不能以C的方式编译多个重载函数。
#include <stdio.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
int func(int a, int b)
{
return a + b;
}
int func(const char* s)
{
return strlen(s);
}
#ifdef __cplusplus
}
#endif
int main(int argc, char *argv[])
{
printf("Press enter to continue ...");
getchar();
return 0;
}
小结:
函数重载是C++对C语言的一个重要的升级。
函数重载通过函数参数列表区分不同的同名函数。
函数的返回值类型不是函数重载的依据。
extern能够实现C和C++的相互调用。
C和C++并不是对立的,可以同时存在于项目中。