C ++の基本的な研究ノート4:オーバーロードされた関数のオーバーロード

C ++の基本的な研究ノート4:オーバーロードされた関数のオーバーロード

関数のオーバーロードとは

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 valueそして、const int valueパラメータが引数を変更しないように違いは、ありません。過負荷ではありません。
  2. char *valueそして、const char *value違い、文字列定数を指す文字列変数、前者点があります。過負荷になる可能性があります。
  3. char *valueそして、char const *value差がない、前者は文字ポインタ定数である文字ポインタ変数、です。パラメーターとして、実際のパラメーター値は変更されません。過負荷ではありません。
  4. int &valueそして、const int &value整数定数を指す整数変数に違い、前者のポインティングがあります。過負荷になる可能性があります。
  5. int &&value&&は右辺値参照を意味します

おすすめ

転載: www.cnblogs.com/realZhaZhaHui/p/12757835.html