詳解C++之函數(shù)重載
函數(shù)重載本質(zhì)
c++中通過(guò)函數(shù)名和函數(shù)確定一個(gè)函數(shù)
所以相同的函數(shù)名,不同參數(shù)也是可以的
不同于c語(yǔ)言,c語(yǔ)言沒(méi)有函數(shù)重載,函數(shù)的本質(zhì)地址就是函數(shù)名
函數(shù)重載發(fā)生在同一個(gè)作用域內(nèi)
類中的重載
構(gòu)造函數(shù)重載
普通成員函數(shù)重載
靜態(tài)成員函數(shù)重載
全局函數(shù)、靜態(tài)成員函數(shù)、普通成員函數(shù)可以發(fā)生重載嗎?
本質(zhì)就是函數(shù)名和函數(shù)參數(shù)不同,并且發(fā)生在同一個(gè)作用域
靜態(tài)函數(shù)和普通成員函數(shù)是可以的
全局函數(shù)作用域在全局作用域,所以不可以
問(wèn)題1:當(dāng)父類的成員函數(shù)和子類的成員函數(shù)相等,會(huì)發(fā)生重載嗎?
本質(zhì)還是上面說(shuō)的,因?yàn)楦割惡妥宇惖淖饔糜虿辉谕粋€(gè)
看一段代碼
#include <iostream>
class father{
public:
father() {
std::cout << "father()" << std::endl;
}
void print() {
std::cout << "father print" << std::endl;
}
};
class child : public father{
public:
child() {
std::cout << "child()" << std::endl;
}
void print(int a) {
std::cout << "child print = " << a << std::endl;
}
};
int main(){
father* father_test = new father();
father_test->print(); //打印father print
child* child_test2 = new child();
child_test2->print(); //編譯錯(cuò)誤no matching function for call to 'child::print()'
return 0;
}
由打印輸出可得第一個(gè)打印屬于father正常輸出,沒(méi)問(wèn)題,第二個(gè)打印是編譯錯(cuò)誤,我們其實(shí)想訪問(wèn)的是父類的print,但是由于子類定義了print,那么在子類的作用域中,print這個(gè)函數(shù)現(xiàn)在只存在一個(gè),那就是void print(int a),如果想調(diào)用父類的就要顯示指定child_test2->father::print();
問(wèn)題2,子類可以重寫父類的函數(shù)嗎,或者說(shuō)定義相同的?
看代碼
#include <iostream>
class father{
public:
father() {
std::cout << "father()" << std::endl;
}
void print() {
std::cout << "father print" << std::endl;
}
};
class child : public father{
public:
child() {
std::cout << "child()" << std::endl;
}
void print() {
std::cout << "child print" << std::endl;
}
};
int main(){
father* father_test = new father();
father_test->print(); //打印father print
child* child_test2 = new child();
child_test2->print(); //child print
return 0;
}
可見(jiàn)是可以定義相同的函數(shù),并重寫的
問(wèn)題3,當(dāng)全局運(yùn)算符重載遇上類中的運(yùn)算符重載,優(yōu)先級(jí)是什么
#include <iostream>
class child;
class father{
public:
father() {
std::cout << "father()" << std::endl;
}
bool operator==(const father& e) {
std::cout << "void print(const father& e)" << std::endl;
}
};
bool operator==(const father& e1, const father& e2) {
std::cout << "void print(const child& e1, const child& e2)" << std::endl;
}
class child : public father{
public:
child() {
std::cout << "child()" << std::endl;
}
};
int main(){
child child1_test;
child child2_test;
child1_test==child2_test;
return 0;
}
輸出為void print(const father& e)類中的運(yùn)算符重載優(yōu)先級(jí)大于全局
當(dāng)復(fù)制兼容遇上全局重載呢?
#include <iostream>
class child;
class father{
public:
father() {
std::cout << "father()" << std::endl;
}
bool operator==(const father& e) {
std::cout << "void print(const father& e)" << std::endl;
}
};
bool operator==(const child& e1, const child& e2) {
std::cout << "void print(const child& e1, const child& e2)" << std::endl;
}
class child : public father{
public:
child() {
std::cout << "child()" << std::endl;
}
};
int main(){
child child1_test;
child child2_test;
child1_test==child2_test;
return 0;
}
打?。?void print(const child& e1, const child& e2)"
僅僅變化了,全局的函數(shù)參數(shù)類型,正常來(lái)說(shuō)類中和全局的==都是可以的,但是當(dāng)類中的需要一個(gè)默認(rèn)的轉(zhuǎn)換,子類到父類,全局的是直接類型就對(duì)的上的,編譯器就優(yōu)先使用全局的
說(shuō)到運(yùn)算符重載,我們現(xiàn)在定義函數(shù)實(shí)現(xiàn)類的功能有三種選擇:成員函數(shù),全局函數(shù),全局函數(shù)+友元函數(shù)
1.看是否需要虛函數(shù),如果需要虛函數(shù),那么肯定是類的成員函數(shù)
2.看是否定義的是<<或者>>運(yùn)算符,我們都知道打印輸出是全局<<,但是為什么呢,看一下實(shí)際的原型針對(duì)String而言ostream& operator<<(ostream& output, const String& string)我們使用的時(shí)候就是 std::cout << "haha";
如果定義為類中的就是ostream& operator<<(ostream& output)使用起來(lái)就是 "haha"<<"std::cout"看出差別了吧,如果定義在類中使用起來(lái)就很別扭,繼續(xù)上面的結(jié)果,如果是<<或者>>運(yùn)算符,然后如果需要訪問(wèn)private域,那么就把全局函數(shù)變?yōu)轭悓?duì)應(yīng)的友元函數(shù)
3.當(dāng)需要對(duì)左邊的參數(shù)進(jìn)行類型轉(zhuǎn)換,需要定義全局函數(shù),因?yàn)轭愔胁僮鞣瘮?shù),左邊的就是類本身,如果需要訪問(wèn)private域,那么就把全局函數(shù)變?yōu)轭悓?duì)應(yīng)的友元函數(shù)
4.其他情況為類的成員函數(shù)
以上就是詳解C++之函數(shù)重載的詳細(xì)內(nèi)容,更多關(guān)于c++之函數(shù)重載的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C語(yǔ)言詳細(xì)實(shí)現(xiàn)猜拳游戲流程
在學(xué)習(xí)了循環(huán)、分支、和函數(shù)之后,可以寫一些簡(jiǎn)單的小游戲來(lái)給自己的編程之路增添一份樂(lè)趣。不僅提升了編碼能力,還可以邊學(xué)邊玩,簡(jiǎn)直妙哉妙哉2022-05-05
C++ Primer Plus 第四章之C++ Primer Plus復(fù)合類型學(xué)習(xí)筆記
數(shù)組(array)是一種數(shù)據(jù)格式,能夠存儲(chǔ)多個(gè)同類型的值。每個(gè)值都存儲(chǔ)在一個(gè)獨(dú)立的數(shù)組元素中,計(jì)算機(jī)在內(nèi)存中依次存儲(chǔ)數(shù)組的各個(gè)元素,今天給大家重點(diǎn)介紹C++ Primer Plus復(fù)合類型的實(shí)例詳解,感興趣的朋友一起看看吧2021-07-07
C++中關(guān)于std::queue?中遇到釋放內(nèi)存錯(cuò)誤的問(wèn)題
這篇文章主要介紹了std::queue中遇到釋放內(nèi)存錯(cuò)誤的問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
C語(yǔ)言實(shí)現(xiàn)兩個(gè)矩陣相乘
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)兩個(gè)矩陣相乘的程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
關(guān)于C++靜態(tài)數(shù)據(jù)成員的實(shí)現(xiàn)講解
今天小編就為大家分享一篇關(guān)于關(guān)于C++靜態(tài)數(shù)據(jù)成員的實(shí)現(xiàn)講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12

