当前位置: 移动技术网 > IT编程>开发语言>C/C++ > C++基础 学习笔记四:重载之函数重载

C++基础 学习笔记四:重载之函数重载

2020年04月23日  | 移动技术网IT编程  | 我要评论

阿诺索西斯,红袋鼠,格力电扇价格

c++基础 学习笔记四:重载之函数重载

什么是函数重载

在c++中允许在同一作用域内声明几个功能类似的同名函数。也就是说用同一个函数完成不同的功能。重载函数是静态多态性的体现。

函数重载的特点

  1. 形式参数(指参数的个数、类型或者顺序)必须不同。函数返回值类型可以相同也可不同。
  2. 匹配函数时并不区分const,引用&,但是const*&结合组成复合运算符时会进行区分,下面会进行代码演示。
  3. 避免了命名空间的污染

函数重载的实现原理

编译程序时编译器会对函数的原始名称进行名称修饰,经过修饰得到的名称来表示函数。

函数重载的使用例子

#include<iostream>

using namespace std;

class printer
{
	private:
		int inkvolume;
		char printertype;
	public:
		printer():inkvolume(0),printertype('z')
			{cout << "print by none-arg function" <<endl;}
			
		printer(int vol):inkvolume(vol),printertype('z')
			{cout << "print by 1-arg function" <<endl;}
			
		printer(int vol, char type):inkvolume(vol),printertype(type)
			{cout << "print by 2-arg function" <<endl;}
			
		//void print(int value){cout << value << " print by function #1" <<endl;}//#1
		
		void print (int value) const {cout << value << " print by const function #2" <<endl;}//#2
		
		void print(int &value){cout << value  << " print by function #3" <<endl;}//#3
		
		void print(const int &value){cout << value  << " print by function #4" <<endl;}//#4
		
		void print(int &&value){cout << value  << " print by function #5" <<endl;}//#5
		
		//int print(int value){cout << value  << " print by function #6" <<endl;return 0;}//#6仅返回值不同,编译不通过
		
		//void print(const int value){cout << value  << " print by function #7" <<endl;}//#7
		
		//void print(int value, int value2 = 1){cout << value << value2  << " print by function #8" <<endl;}//#8默认参数在后 
			
		void print(float value){cout << value  << " print by function #9" <<endl;}//#9
		
		void print(char value){cout << value  << " print by function #10" <<endl;}//#10
		
		void print(char* value){cout << *value  << " print by function #11" <<endl;}//#11
		
		void print(const char* value){cout << *value  << " print by function #12" <<endl;}//#12
		
		//void print(char* const value){cout << value  << " print by function #13" <<endl;}//#13
};

int main()
{
	printer printer1;
	printer printer2(123);
	const printer printer3(123,'a');
	
	int intvalue = 123;
	const int c_intvalue = 1234;
	float floatvalue = 1.1;
	char charvalue = 'a';
	char* p_charvalue = new char('b');
	const char* cp_charvalue = new char('c');
	
	
	//printer1.print(1);//1 是 立即数常量 可以调用#1,#4,#5 ,#2(当且仅当仅存在#2时) 
	printer3.print(1);//只调用 #2
	printer1.print(intvalue);//#3 
	printer1.print(c_intvalue);//#4 
	printer1.print(1+1);//#5 
	printer1.print(floatvalue);//#9
	printer1.print(charvalue);//#10 
	printer1.print(p_charvalue);//#11
	printer1.print(cp_charvalue);//#12
	
    return 0;
}
/* 运行结果为: 
print by none-arg function
print by 1-arg function
print by 2-arg function
1 print by const function #2
123 print by function #3
1234 print by function #4
2 print by function #5
1.1 print by function #9
a print by function #10
b print by function #11
c print by function #12

--------------------------------
process exited after 0.09048 seconds with return value 0
请按任意键继续. . .
*/

代码分析

  1. int valueconst int value没有区别,作为参数时都不会改变实参的值。不可重载。
  2. char *valueconst char *value是有区别的,前者指向一个字符串变量,后者指向一个字符串常量。可重载。
  3. char *valuechar const *value没有区别,前者为字符指针变量,后者为字符指针常量。作为参数时都不会改变实参的值。不可重载。
  4. int &valueconst int &value是有区别的,前者指向一个整型变量,后者指向一个整型常量。可重载。
  5. int &&value&&表示右值引用

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网