函数重载
C++支持函数重载,即同名函数可以根据参数的不同而有不同的实现。
#include <iostream>
using namespace std;
void func(int a){
cout<<"a 是类型 "<<typeid(a).name()<<endl;
}
void func(char a){
cout<<"a 是类型 "<<typeid(a).name()<<endl;
}
int main(){
int a;
func(a);
char b;
func(b);
return 0;
}
输出:
a 是类型 i
a 是类型 c
构成重载的规则
- 函数名相同
- 参数个数不同:参数的类型不同:参数顺序不同;
- 返回值类型,不作为重载的标准。
#include <iostream>
using namespace std;
//函数重载 (静多态(Polymorphism),在编译阶段确定了)
void print(int a){ //倾轧->void print_i(int a)
cout << "函数:void print(int a)执行"<< endl;
}
void print(char b){ //倾轧->void print_c(char b)
cout << "函数:void print(char b)执行"<< endl;
}
void print(int a, char b){ //倾轧->void print_ic(int a, char b)
cout << "函数:void print(int a, char b)执行"<< endl;
}
void print( char b,int a){ //倾轧->void print_ci(char b,int a)
cout << "函数:void print(char b,int a)执行"<< endl;
}
int main(){
int a;
char b;
print(a);
print(b);
print(a,b);
print(b,a);
return 0;
}
运行结果
函数:void print(int a)执行
函数:void print(char b)执行
函数:void print(int a, char b)执行
函数:void print(char b,int a)执行
匹配规则
- 严格匹配,找到则调用。
- 通过隐式转换寻求一个匹配,找到则调用。
C++ 允许, int 到 long 和 double, double 到 int 和 float, int 到
short 和 char 等隐式类型转换。遇到这种情型,则会引起二义性。(ambiguous)
error: call of overloaded 'print(int)' is ambiguous
print(1); // print(int)
error: call of overloaded 'print(char)' is ambiguous
print('a'); // print(int)
重载底层实现
C++利用 Name Mangling(命名倾轧)技术,来改变函数名,区分参数不同的同名函数。
实现原理:用 v-c- i-f- l- d 表示 void char int float long double
及其引用。具体平台,实现有差异。
extern “C”
C++ 完全兼容 c 语言,那就面临着,完全兼容 C 的类库。由.c 文件的类库文件中
函数名,并没有发生 name mangling 行为,而我们在包含.cpp 文件所对应的.h 文件
时,.h 文件要发生 name manling 行为,因而会在链接的时候发生的错误。
C++为了避免上述错误的发生,重载了关键字 extern。只需要在避免 name
manling 的函数前,加 extern “C” 如有多个,则 extern “C”{}
Op Overload 运算符重载
前面用到的<<本身在 C 语言中是位操作中的左移运算符。现在又用作流插入运算
符,这种一个运算符多种用处的现像叫作重载。
在 C 语言中本身就用重载的现像,比如 & 既表示取地址,又表示位操作中的与。
*既表示解引用,又表示乘法运算符。只不过 c 语言并没有开放重载机制。
C++提供了运算符重载机制。可以为自定义数据类型重载运算符。实现构造数据类
型也可以像基本数据类型一样的运算特性。
#include<iostream>
using namespace std;
struct Comp
{
float real;
float image;
};
Comp operator + (Comp one, Comp another)
{
one.real += another.real;
one.image += another.image;
return one;
}
int main()
{
Comp c1 = {1,2};
Comp c2 = {3,4};
Comp sum = c1+c2; //operator+(c1,c2);
cout<<sum.real<<" "<<sum.image<<endl;
return 0;
}
示例中重载了一个全局的操作符+号用于实现将两个自定义结构体类型相加。本质
是函数的调用。
当然这个 Comp operator+(Comp one, Comp another),也可以定义为 Comp
add(Comp one, Comp another),但这样的话,就只能 Comp sum = add(c1,c2),
而不能实现 Comp sum = c1 +c2 了
Default Arg 默认参数
通常情况下,函数在调用时,形参从实参那里取得值。C++给出了可以不用从实参
取值的方法,给形参以默认值。
默认值,则是一种最通常的情况。是对真实生活的模似,生活中很找出没有默认值
的东西。
故,C++引入默认参数,也是为了方便编程。
单个参数:
#include <iostream>
#include <time.h>
using namespace std;
void weatherForcast(char * w="sunny") //默认参数
{
time_t t = time(0);
char tmp[64];
strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A ",localtime(&t) );
cout<<tmp<< "today is weahter "<<w<<endl;
}
int main()
{
//sunny windy cloudy foggy rainy
weatherForcast();//不传参数将使用默认参数
weatherForcast("rainny");
weatherForcast();//不传参数将使用默认参数
return 0;
}
输出:
2024/07/21 17:09:11 Sunday today is weahter sunny
2024/07/21 17:09:11 Sunday today is weahter rainny
2024/07/21 17:09:11 Sunday today is weahter sunny
多个参数:
#include <iostream>
#include <time.h>
using namespace std;
//从右向左默认,中间不能跳跃
//void print(int a,int b=2,int c=3);
//void print(int a=1,int b=2,int c); //错误
//void print(int a=1,int b,int c=3); //错误
void print(int a=1,int b=2,int c=3); //正确,声明在前,定义在后,默认参数只能在声明处。
void print(int a,int b,int c)
{
cout<<"a="<<a<<" b="<<b<<" c="<<c<<endl;
}
int main()
{
print(); //错误
print(5); //正确
print(7,8,9); //正确
return 0;
}
默认规则:
- 默认的顺序,是从右向左,不能跳跃。
- 若函数声明和定义一体时,默认认参数在定义(声明)处。 声明在前,定义在后,默认参数只能在声明处。
- 默认值可以是常量,全局变量,或是一个函数。
- 实参个数 + 默认参数的个数 >= 形参个数
规则冲突(conflict)
一个函数,不能既作重载,又作默认参数的函数。当你少写一个参数时,系统无
法确认是重载还是默认参数。
当两者要实现同样的功能时,优先选用默认参数。
#include <iostream>
using namespace std;
void print(int a)
{
}
void print(int a,int b =10)
{
}
int main()
{
print(10);
return 0;
}
代码将报错
A:/C++/CDemo/main.cpp:11:13: error: call of overloaded 'print(int)' is ambiguous
print(10);
^