從string類(lèi)的實(shí)現(xiàn)看C++類(lèi)的四大函數(shù)(面試常見(jiàn))
朋友面試的一道面試題,分享給大家,面試官經(jīng)常會(huì)問(wèn)到的,實(shí)現(xiàn)string類(lèi)的四大基本函數(shù)必掌握。
一個(gè)C++類(lèi)一般至少有四大函數(shù),即構(gòu)造函數(shù)、拷貝構(gòu)造函數(shù)、析構(gòu)函數(shù)和賦值函數(shù),一般系統(tǒng)都會(huì)默認(rèn)。但是往往系統(tǒng)默認(rèn)的并不是我們所期望的,為此我們就有必要自己創(chuàng)造他們。在創(chuàng)造之前必須了解他們的作用和意義,做到有的放矢才能寫(xiě)出有效的函數(shù)。
#include <iostream> class CString { friend std::ostream & operator<<(std::ostream &, CString &); public: // 無(wú)參數(shù)的構(gòu)造函數(shù) CString(); // 帶參數(shù)的構(gòu)造函數(shù) CString(char *pStr); // 拷貝構(gòu)造函數(shù) CString(const CString &sStr); // 析構(gòu)函數(shù) ~CString(); // 賦值運(yùn)算符重載 CString & operator=(const CString & sStr); private: char *m_pContent; }; inline CString::CString() { printf("NULL\n"); m_pContent = NULL; m_pContent = new char[1]; m_pContent[0] = '\0'; } inline CString::CString(char *pStr) { printf("use value Contru\n"); m_pContent = new char[strlen(pStr) + 1]; strcpy(m_pContent, pStr); } inline CString::CString(const CString &sStr) { printf("use copy Contru\n"); if(sStr.m_pContent == NULL) m_pContent == NULL; else { m_pContent = new char[strlen(sStr.m_pContent) + 1]; strcpy(m_pContent, sStr.m_pContent); } } inline CString::~CString() { printf("use ~ \n"); if(m_pContent != NULL) delete [] m_pContent; } inline CString & CString::operator = (const CString &sStr) { printf("use operator = \n"); if(this == &sStr) return *this; // 順序很重要,為了防止內(nèi)存申請(qǐng)失敗后,m_pContent為NULL char *pTempStr = new char[strlen(sStr.m_pContent) + 1]; delete [] m_pContent; m_pContent = NULL; m_pContent = pTempStr; strcpy(m_pContent, sStr.m_pContent); return *this; } std::ostream & operator<<(std::ostream &os, CString & str) { os<<str.m_pContent; return os; } int main() { CString str3; // 調(diào)用無(wú)參數(shù)的構(gòu)造函數(shù) CString str = "My CString!"; // 聲明字符串,相當(dāng)于調(diào)用構(gòu)造函數(shù) std::cout<<str<<std::endl; CString str2 = str; // 聲明字符串,相當(dāng)于調(diào)用構(gòu)造函數(shù) std::cout<<str2<<std::endl; str2 = str; // 調(diào)用重載的賦值運(yùn)算符 std::cout<<str2<<std::endl; return 0; }
輸出:
NULL
use value Contru
My CString!
use copy Contru
My CString!
use operator =
My CString!
use ~
use ~
use ~
以上所述是小編給大家介紹的從string類(lèi)的實(shí)現(xiàn)看C++類(lèi)的四大函數(shù)(面試常見(jiàn))的全部敘述,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 詳解C++中String類(lèi)模擬實(shí)現(xiàn)以及深拷貝淺拷貝
- 自己模擬寫(xiě)C++中的String類(lèi)型實(shí)例講解
- 詳解C++的String類(lèi)的字符串分割實(shí)現(xiàn)
- C++實(shí)現(xiàn)String類(lèi)實(shí)例代碼
- C++中將string類(lèi)型轉(zhuǎn)化為int類(lèi)型
- 詳解C++中實(shí)現(xiàn)繼承string類(lèi)的MyString類(lèi)的步驟
- 探究C++中string類(lèi)的實(shí)現(xiàn)原理以及擴(kuò)展使用
- C++中的string類(lèi)的用法小結(jié)
- 分享C++面試中string類(lèi)的一種正確寫(xiě)法
- 利用C++實(shí)現(xiàn)從std::string類(lèi)型到bool型的轉(zhuǎn)換
- c++中string類(lèi)成員函數(shù)c_str()的用法
- 代碼分析c++中string類(lèi)
相關(guān)文章
字符串的組合算法問(wèn)題的C語(yǔ)言實(shí)現(xiàn)攻略
這篇文章主要介紹了字符串的組合算法問(wèn)題的C語(yǔ)言實(shí)現(xiàn)攻略,是根據(jù)ACM總結(jié)的經(jīng)典算法問(wèn)題,需要的朋友可以參考下2015-08-08C語(yǔ)言的isatty函數(shù)和ttyname函數(shù)以及sendmsg函數(shù)用法
這篇文章主要介紹了C語(yǔ)言的isatty函數(shù)和ttyname函數(shù)以及sendmsg函數(shù)用法,是C語(yǔ)言入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09C語(yǔ)言的字符函數(shù)和字符串函數(shù)詳解
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言的字符函數(shù)和字符串函數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03VC實(shí)現(xiàn)獲取當(dāng)前正在運(yùn)行的進(jìn)程
這篇文章主要介紹了VC實(shí)現(xiàn)獲取當(dāng)前正在運(yùn)行的進(jìn)程,涉及VC針對(duì)系統(tǒng)進(jìn)程的相關(guān)操作技巧,需要的朋友可以參考下2015-05-05C語(yǔ)言在輸入輸出時(shí)遇到的常見(jiàn)問(wèn)題總結(jié)
大家在平時(shí)的做題中是否會(huì)遇到和我一樣的煩惱,題目的代碼已經(jīng)基本完成,但是在輸出時(shí)候,總是和題目給出的樣例輸出格式不同?,導(dǎo)致題目不能通過(guò)。為了解決這一煩惱,我總結(jié)了以下幾點(diǎn),需要的可以參考一下2022-09-09C++控制臺(tái)循環(huán)鏈表實(shí)現(xiàn)貪吃蛇
這篇文章主要為大家詳細(xì)介紹了C++控制臺(tái)循環(huán)鏈表實(shí)現(xiàn)貪吃蛇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04