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 = https://www.cnblogs.com/realZhaZhaHui/p/123;
const int c_intValue = 1234;
float floatValue = 1.1;
char charValue ='A';
char* p_charValue = https://www.cnblogs.com/realZhaZhaHui/p/new char('B');
const char* cp_charValue = https://www.cnblogs.com/realZhaZhaHui/p/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&&表示右值參考
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/36255.html
標籤:C++
下一篇:成員指標與mem_fn
