劍指Offer 賦值運算子函式

2021-06-26 19:08:32 字數 1273 閱讀 7096

按照自己淺薄的理解,敲了如下**
#include using namespace std;
class cmystring

就是定義了!

cmystring(char *m_pchar = null);

cmystring(const cmystring &other);

~cmystring();

cmystring& operator=(const cmystring &other);

};cmystring& cmystring::operator=(const cmystring &other)

;int main()

程式報錯

1>test_sizeof.obj : error lnk2019: 無法解析的外部符號 "public: __thiscall cmystring::~cmystring(void)" (??1cmystring@@qae@xz),該符號在函式 _main 中被引用1>test_sizeof.obj : error lnk2019: 無法解析的外部符號 "public: __thiscall cmystring::cmystring(class cmystring const &)" (??0cmystring@@qae@abv0@@z),該符號在函式 _main 中被引用1>test_sizeof.obj : error lnk2019: 無法解析的外部符號 "public: __thiscall cmystring::cmystring(char *)" (??0cmystring@@qae@pad@z),該符號在函式 _main 中被引用1> d:\myprogram\exercise\exc10\debug\sizeof.exe : fatal error lnk1120: 3 個無法解析的外部命令

原因僅給出了函式宣告,未寫函式的定義,修改如下

class cmystring

cmystring(char *pchar)

cmystring(const cmystring &other){}

~cmystring()

cmystring& operator=(const cmystring &other);

};cmystring& cmystring::operator=(const cmystring &other)

;int main()

劍指offer 賦值運算子函式

題目 如下為型別cmystring的宣告,請為該型別新增賦值運算子函式 class cmystring 思路 類的定義和基本形式 類其實是定義乙個特殊的資料結構,定義了類的物件包括了什麼,以及可以在這個物件上執行哪些操作 class student 成員函式定義 student student vo...

劍指offer 賦值運算子過載

還是有很多點需要注意的。判斷this與傳入的object是否是同乙個物件,相同的話直接返回 this就可以了。判斷傳入物件的陣列是否為空,空的話就不必複製,直接釋放記憶體並將指標置null即可。考慮異常安全。因為new char時如果記憶體不足會丟擲異常,這時安全的做法是先嘗試申請記憶體,申請成功後...

劍指offer1 賦值運算子函式

include using namespace std class cmystring cmystring const cmystring str cmystring operator const cmystring str int display cmystring private char m ...