我是靠谱客的博主 迷路雪糕,这篇文章主要介绍C++之函数的重载,现在分享给大家,希望可以做个参考。

一、C++ 函数重载

C++ 致力于简化编程,能过函数重名来达到简化编程的目的

1.重载规则

1.函数名相同

2.参数的个数不同,类型不同,顺序不同,都可以构成重载

3.返回值类型必须相同,不同则不可以构成重载

例如:

复制代码
1
2
3
4
5
void func(int a); //ok void func(char a); //ok void func(char a,int b); //ok void func(int a, char b); //ok char func(int a); //与第一个函数有冲突

2.匹配原则

1.严格匹配,找到则调用

2.通过隐式转换寻求一个匹配,找到则调用

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream> #include <iomanip> using namespace std; void print(double a){ cout<<a<<endl; } void print(int a){ cout<<a<<endl; } int main(){ print(1); // print(int) print(1.1); // print(double) print('a'); // print(int) print(1.11f); // print(double) return 0; }


C++ 允许,int 到 long 、double,double 到 int 、float 隐式类型转换。遇到这种情型,则会引起二义性

例:将上题上的 print(int a)中的类型 int 改为double

复制代码
1
2
3
4
rror: call of overloaded 'print(int)' is ambiguous print(1); // print(int) error: call of overloaded 'print(char)' is ambiguous print('a');

解决方法,在调用时强转

3.重载底层实现

C++利用 name mangling(倾轧)技术,来改名函数名,区分参数不同的同名函数。

实现原理:用 v-c-i-f-l-d 表示 void char int float long double 及其引用。

复制代码
1
2
void func(char a); // func_c(char a) void func(char a, int b, double c);//func_cid(char a, int b, double c)

4.extern “C”

name mangling 发生在两个阶段,.cpp 编译阶段,和.h 的声明阶段。

只有两个阶段同时进行,才能匹配调用。

mystring.h

复制代码
1
2
3
extern "C"{ int myStrlen(char *str); }

mystring.cpp

复制代码
1
2
3
4
5
6
7
8
#include "mystring.h" int myStrlen(char *str) { int len = 0; while(*str++) len++; return len; }

main.cpp

复制代码
1
2
3
4
5
6
7
8
9
10
#include <iostream> #include "mystring.h" using namespace std; int main() { char *p = "china"; int len; len = myStrlen(p); return 0; }

C++ 完全兼容 c 语言,那就面临着,完全兼容 c 的类库。由.c 文件的类库文件中函数名,并没有发生 name mangling 行为,而我们在包含.c 文件所对应的.h 文件时,.h 文件要发生name manling 行为,因而会发生在链接的时候的错误。C++为了避免上述错误的发生,重载了关键字 extern。只需在要避免 name manling的函数前,加 extern “C” 如有多个,则 extern “C”{}

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注靠谱客的更多内容!

最后

以上就是迷路雪糕最近收集整理的关于C++之函数的重载的全部内容,更多相关C++之函数内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(142)

评论列表共有 0 条评论

立即
投稿
返回
顶部