C++基礎 學習筆記四:重載之函數重載 什麼是函數重載 在C++中允許在同一作用域內聲明幾個功能類似的同名函數。也就是說用同一個函數完成不同的功能。重載函數是靜態多態性的體現。 函數重載的特點 1. 形式參數(指參數的個數、類型或者順序)必須不同。函數返回值類型可以相同也可不同。 2. 匹配函數時並 ...
C++基礎 學習筆記四:重載之函數重載
什麼是函數重載
在C++中允許在同一作用域內聲明幾個功能類似的同名函數。也就是說用同一個函數完成不同的功能。重載函數是靜態多態性的體現。
函數重載的特點
- 形式參數(指參數的個數、類型或者順序)必須不同。函數返回值類型可以相同也可不同。
- 匹配函數時並不區分
const
,引用&
,但是const
與*
或&
結合組成複合運算符時會進行區分,下麵會進行代碼演示。 - 避免了命名空間的污染
函數重載的實現原理
編譯程式時編譯器會對函數的原始名稱進行名稱修飾
,經過修飾得到的名稱來表示函數。
函數重載的使用例子
#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
請按任意鍵繼續. . .
*/
代碼分析
int value
和const int value
沒有區別,作為參數時都不會改變實參的值。不可重載。char *value
和const char *value
是有區別的,前者指向一個字元串變數,後者指向一個字元串常量。可重載。char *value
和char const *value
沒有區別,前者為字元指針變數,後者為字元指針常量。作為參數時都不會改變實參的值。不可重載。int &value
和const int &value
是有區別的,前者指向一個整型變數,後者指向一個整型常量。可重載。int &&value
&&表示右值引用