從string類的實(shí)現(xiàn)看C++類的四大函數(shù)(面試常見)
朋友面試的一道面試題,分享給大家,面試官經(jīng)常會(huì)問到的,實(shí)現(xiàn)string類的四大基本函數(shù)必掌握。
一個(gè)C++類一般至少有四大函數(shù),即構(gòu)造函數(shù)、拷貝構(gòu)造函數(shù)、析構(gòu)函數(shù)和賦值函數(shù),一般系統(tǒng)都會(huì)默認(rèn)。但是往往系統(tǒng)默認(rèn)的并不是我們所期望的,為此我們就有必要自己創(chuàng)造他們。在創(chuàng)造之前必須了解他們的作用和意義,做到有的放矢才能寫出有效的函數(shù)。
#include <iostream>
class CString
{
friend std::ostream & operator<<(std::ostream &, CString &);
public:
// 無參數(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)用無參數(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類的實(shí)現(xiàn)看C++類的四大函數(shù)(面試常見)的全部敘述,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 詳解C++中String類模擬實(shí)現(xiàn)以及深拷貝淺拷貝
- 自己模擬寫C++中的String類型實(shí)例講解
- 詳解C++的String類的字符串分割實(shí)現(xiàn)
- C++實(shí)現(xiàn)String類實(shí)例代碼
- C++中將string類型轉(zhuǎn)化為int類型
- 詳解C++中實(shí)現(xiàn)繼承string類的MyString類的步驟
- 探究C++中string類的實(shí)現(xiàn)原理以及擴(kuò)展使用
- C++中的string類的用法小結(jié)
- 分享C++面試中string類的一種正確寫法
- 利用C++實(shí)現(xiàn)從std::string類型到bool型的轉(zhuǎn)換
- c++中string類成員函數(shù)c_str()的用法
- 代碼分析c++中string類
相關(guān)文章
C++結(jié)構(gòu)體中變長(zhǎng)數(shù)組的使用問題分解刨析
變長(zhǎng)數(shù)組在C++中指的是集合(也叫容器)如vector就是C語言中,所有的數(shù)組都不定長(zhǎng),沒有下標(biāo)越界的概念,數(shù)組實(shí)質(zhì)就是一個(gè)指針(由數(shù)組名充當(dāng))因此C語言中數(shù)組的長(zhǎng)度沒有任何意義平常在C語言中講的不定長(zhǎng)數(shù)組,其實(shí)就是指針2022-08-08
深入C++實(shí)現(xiàn)函數(shù)itoa()的分析
本篇文章是對(duì)C++實(shí)現(xiàn)函數(shù)itoa()進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C語言實(shí)現(xiàn)socket簡(jiǎn)單通信實(shí)例
這篇文章主要介紹了C語言實(shí)現(xiàn)socket簡(jiǎn)單通信的方法,是學(xué)習(xí)C語言網(wǎng)絡(luò)編程非?;A(chǔ)而又實(shí)用的實(shí)例,需要的朋友可以參考下2014-09-09
C語言實(shí)現(xiàn)點(diǎn)菜系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)點(diǎn)菜系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06

