C 自定義String的實現

2021-10-05 10:22:47 字數 689 閱讀 9002

這個在面試或筆試的時候常問到或考到。

已知類string的原型為:

class string

;

請編寫string的上述4個函式。

//普通建構函式  

string::string(const char *str)

else }

// string的析構函式

string::~string(void)

//拷貝建構函式

string::string(const string &other)// 得分點:輸入引數為const型

//賦值函式

string & string::operator = (const string &other) // 得分點:輸入引數為const型

剖析:

能夠準確無誤地編寫出string類的建構函式、拷貝建構函式、賦值函式和析構函式的面試者至少已經具備了c++基本功的60%以上!

在這個類中包括了指標類成員變數m_data,當類中包括指標類成員變數時,一定要過載其拷貝建構函式、賦值函式和析構函式,這既是對c++程式設計師的基本要求,也是《effective c++》中特別強調的條款。

仔細學習這個類,特別注意加注釋的得分點和加分點的意義,這樣就具備了60%以上的c++基本功!

C 實現自定義string類

在一些c 筆試題裡,會有這樣一道題,那就是讓你自己實現乙個簡單的string類。自己在面試的時候就遇到過這個題。在這裡說一下自己是怎麼做的。主要包含一些基本的操作,建構函式 拷貝建構函式和析構函式。pragma once include using namespace std class mystr...

c 自定義string類

1.標頭檔案部分 define crt secure no warnings pragma once include includeusing namespace std class mystring 2.函式定義部分 include mystring.h mystring mystring mys...

自定義string類的簡單實現

大家都知道c 中有乙個string類,由於正在學習類,就模仿c 中的string類寫了乙個自定義的string類,下面是自定義string類的簡單實現 fedora下實現 ifndef mystring define mystring include using namespace std clas...