十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
1、友元函數(shù)
成都網(wǎng)絡(luò)公司-成都網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)十余年經(jīng)驗(yàn)成就非凡,專業(yè)從事網(wǎng)站制作、成都做網(wǎng)站,成都網(wǎng)頁設(shè)計(jì),成都網(wǎng)頁制作,軟文發(fā)布平臺,廣告投放等。十余年來已成功提供全面的成都網(wǎng)站建設(shè)方案,打造行業(yè)特色的成都網(wǎng)站建設(shè)案例,建站熱線:18982081108,我們期待您的來電!
在類中一般的成員函數(shù)具備:
(1)、該函數(shù)能訪問類中的私有部分
(2)、該函數(shù)位于類的作用域之中
(3)、該函數(shù)必須經(jīng)由一個(gè)對象去激活,也就是說有一個(gè)this指針;
友元函數(shù)不同之處:
(1)、在#include
class Test; void fun(); //有元函數(shù)的聲明
但是在#include
(2)、在類內(nèi)必須聲明是朋友,friend(關(guān)鍵字),函數(shù)聲明
class Test{ friend void fun(); //朋友沒有公有,私有,保護(hù)之分,所以限定修飾符在這不起任何作用 };
(3)、在類外實(shí)現(xiàn)方法的定義,此時(shí)不用再加friend了;
void fun(){ .......... }
有元函數(shù)具有的特性:該函數(shù)能訪問類的私有數(shù)據(jù);不是該類的成員函數(shù),也不需要對象驅(qū)動(dòng);
還有友元類,跟其使用一樣,類中所有函數(shù)均可調(diào)用其朋友的私有數(shù)據(jù);
2、運(yùn)算符的重載(個(gè)人認(rèn)為C++中運(yùn)算符的重載很有用,也很重要)
相當(dāng)于函數(shù)的調(diào)用,對其的另一種更加人文化的解釋,并告訴 C++編譯器,遇到該重載運(yùn)算符時(shí)調(diào)用此函數(shù)。
定義運(yùn)算符重載的一般格式:
返回值類型 類名::operator重載的運(yùn)算符(參數(shù)列表);
假如出現(xiàn)復(fù)數(shù)類:
Complex{ public: ..... private: int real; int p_w_picpath; };
//主函數(shù)中: Complex t1(1, 2); Complex t2(2, 3); Complex t3; t3 = t1 + t2;
此時(shí)t1和t2的類型不定,就得對+進(jìn)行運(yùn)算符的重載;使其更符合人文化,其本質(zhì)不變,t1.operator+(t2);
對象的類型不定,編譯器不知道怎么輸出,自己就得對<<和>>運(yùn)算符進(jìn)行重載,以達(dá)到輸出對象的目的。還必須是友元函數(shù);
class Complex; //這是第一步,類外聲明。 ostream& operator<<(ostream &out, const Complex &c); istream& operator>>(istream &in, Complex &c); class Complex{ //這是第二步,對其在類內(nèi)說明是朋友。 friend ostream& operator<<(ostream &out, const Complex &c); friend istream& operator>>(istream &in, Complex &c); }; ostream& operator<<(ostream &out, const Complex &c){ out<<"("<>(istream &in, Complex &c){ in>>c.m_real>>c.m_imag; return in; }
關(guān)于這個(gè)<<對應(yīng)一個(gè)要輸出的值,在定義時(shí),不用endl,回車。
對于Complate類的補(bǔ)充,t1 = t2 + 10; //成員方法重載即可。內(nèi)部有一個(gè)隱藏參數(shù)
t1 = 10 + t2; //沒有對象驅(qū)動(dòng),還要10.operator+(t2);是錯(cuò)的,所以此時(shí)用友元函數(shù),傳兩個(gè)參數(shù)就是有兩個(gè)參數(shù)。
運(yùn)算符的重載要注意:
(1)、operator后面是一個(gè)合法的運(yùn)算符,將右操作數(shù)做為函數(shù)的實(shí)參;
(2)、++ --前后的問題,用函數(shù)重載區(qū)分,后加的(int)參數(shù);
(3)、?: . .* :: sizeof不能重載,其它的應(yīng)該都可以進(jìn)行重載;
例1:++i 和 i++有什么區(qū)別?
對其進(jìn)行重載,如下:
#includeusing namespace std; class Int; ostream& operator<<(ostream &out, const Int &s); class Int{ friend ostream& operator<<(ostream &out, const Int &s); public: Int(int m = 0) : t(m){} ~Int(){} public: Int& operator++()//++t 先加1,再將此對象的引用返回 { t++; return *this; } Int operator++(int) //t++ 后加的要有一個(gè)參數(shù)int區(qū)分, { Int tmp(t); //創(chuàng)建一個(gè)臨時(shí)對象, t++; //原有對象加了一下, return tmp; //返回臨時(shí)對象,后加。 } private: int t; }; ostream& operator<<(ostream &out, const Int &s){ out< 運(yùn)行結(jié)果如下:
從結(jié)果可以看出,確是實(shí)現(xiàn)了,這塊的本質(zhì)是運(yùn)算符對++的重載。
前加,返回的是引用,后加還得調(diào)用構(gòu)造函數(shù)和拷貝構(gòu)造;所以前加++i;效率更高,運(yùn)行速度也更快。
例2、String類
#include#include #include using namespace std; class String; ostream& operator<<(ostream &out, const String &str); class String{ friend ostream& operator<<(ostream &out, const String &str); public: String(const char *str = ""){ if(str == NULL){ data = new char[1]; data[0] = 0; }else{ data = new char[strlen(str) + 1]; strcpy(data, str); } } //String(const String &str){} String& operator=(const String &str){ if(this != &str){ delete []data; data = new char[strlen(str.data) + 1]; strcpy(data, str.data); } return *this; } ~String(){ delete []data; } public: String operator+(const String &str){ char *tmp; tmp = new char[strlen(data) + strlen(str.data) + 1]; strcpy(tmp, data); strcat(tmp, str.data); return String(tmp); } void operator+=(const String &str){ char *new_data = new char[strlen(data) + strlen(str.data) + 1]; strcpy(new_data, data); strcat(new_data, str.data); delete []data; data = new_data; /* realloc(data, strlen(data) + strlen(str.data) + 1); //此時(shí)對原有的數(shù)組空間進(jìn)行動(dòng)態(tài)擴(kuò)長, // 給誰擴(kuò)長 加上原先,一共要有多長 即原有值不變。 strcat(data, str.data); */ } char operator[](int pos){ assert(pos >= 0 && pos < strlen(data)); return data[pos]; } char operator*(){ return data[0]; } bool operator==(const String &str){ return strcmp(data, str.data) ? false : true; } bool operator!=(const String &str){ return !(*this == str); //調(diào)用==這個(gè)編寫過的函數(shù) } private: char *data; }; ostream& operator<<(ostream &out, const String &str){ out< 對其+,+=,[],*,==,!=進(jìn)行了運(yùn)算符的重載,結(jié)果如下:
3、const和static在C++中const是非常重要的,現(xiàn)在說明常函數(shù)之間的關(guān)系:普通函數(shù)可以調(diào)用常函數(shù),常函數(shù)不能調(diào)用普通函數(shù)(因?yàn)闆]有this);
void fun()const{}
void fun(){} //一個(gè)有const,一個(gè)沒有,參數(shù)類型不一樣,所以這是函數(shù)的重載!
static在C++中有成員和方法,靜態(tài)成員的在類外初始化;類的靜態(tài)成員為所有對象所共享。
靜態(tài)方法: (1)、可以調(diào)用該類的私有成員,(2)、應(yīng)該位于類的作用域之中;(3)、類名直接調(diào)用,可以不用方法;
靜態(tài)方法只能調(diào)用靜態(tài)成員,和靜態(tài)函數(shù);非靜態(tài)的可以調(diào)用靜態(tài)成員或方法。
標(biāo)題名稱:友元函數(shù)運(yùn)算符重載conststatic
當(dāng)前鏈接:http://m.jiaotiyi.com/article/ipdisd.html