02-函数重载

14 篇文章 0 订阅
11 篇文章 0 订阅
#include <iostream>
using namespace std;

/*
	C语言不支持函数重载
	C++支持函数重载
*/
//函数重载
//函数名相同
//参数个数不同、参数类型不同、参数顺序不同
//重载的本质
//采用了name mangling或者叫name decoration技术
// C++编译器默认会对符号名(比如函数名)进行改编、修饰,有些地方翻译为“命名倾轧”
// 重载时会生成多个不同的函数名,不同编译器(MSVC、g++)有不同的生成规则
// 通过IDA打开【VS_Release_禁止优化】可以看到
int sum(int v1, int v2) {
	return v1 + v2;
}

int sum(int v1, int v2, int v3) {
	return v1 + v2 + v3;
}

void func(int v1, double v2) {
	cout << "func(int v1, double v2)" << endl;
}

void func(double v1, int v2) {
	cout << "func(double v1, int v2)" << endl;
}

//返回值类型与函数重载无关

// 歧义,二义性
int func(int a)
 {
	return 0;
}

double func()
 {
	return 0;
}

//调用函数时,实参的隐式类型转换可能会产生二义性
// display_v
void display() {
	cout << "display()" << endl;
}

// display_i
void display(int a) {
	cout << "display(int) - " << a << endl;
}

// display_l
void display(long a) {
	cout << "display(long) - " << a << endl;
}

// display_d
void display(double a) {
	cout << "display(double) - " << a << endl;
}

// Debug模式:很多调试信息,生成的可执行文件比较臃肿
// Release模式:去除调试信息,生成的可执行文件比较精简、高效

// 反汇编
// 逆向工程
int main() {
	//调用函数时,实参的隐式类型转换可能会产生二义性
	int a = 10;
	long a = 10;
	double a = 10;
	display();
	display(10);
	display(10L);
	display(10.0); 

	// func(10, 10.5);
	// func(10.5, 10);

	// cout << sum(10, 20) << endl;
	// cout << sum(10, 20, 30) << endl;

	getchar();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值