編寫類String的建構函式、解構函式和賦值函式,已知類String的原型為:
class String
{
public:
String(const char *str = NULL); // 普通建構函式
String(const String &other); // 拷貝建構函式
~ String(void); // 解構函式
String & operate =(const String &other); // 賦值函式
private:
char *m_data; // 用於儲存字串
};
解答:
//普通建構函式
String::String(const char *str)
{
if(str==NULL)
{
m_data = new char[1]; // 得分點:對空字串自動申請存放結束標誌'