標準答案:
// String的析構函數
String::~String(void) // 3分
{
delete [] m_data;
// 由于m_data是內部數據類型,也可以寫成 delete m_data;
}
// String的普通構造函數
String::String(const char *str) // 6分
{
if(str==NULL)
{
m_data = new char[1]; // 若能加 NULL 判斷則更好
*m_data = ‘\0’;
}
else
{
int length = strlen(str);
m_data = new char[length+1]; // 若能加 NULL 判斷則更好
strcpy(m_data, str);
}
}
// 拷貝構造函數
String::String(const String &other) // 3分
{
int length = strlen(other.m_data);
m_data = new char[length+1]; // 若能加 NULL 判斷則更好
strcpy(m_data, other.m_data);
}
// 賦值函數
String & String::operate =(const String &other) // 13分
{
// (1) 檢查自賦值 // 4分
if(this == &other)
return *this;
// (2) 釋放原有的內存資源 // 3分
delete [] m_data;
// (3)分配新的內存資源,并復制內容 // 3分
int length = strlen(other.m_data);
m_data = new char[length+1]; // 若能加 NULL 判斷則更好
strcpy(m_data, other.m_data);
// (4)返回本對象的引用 // 3分
金山職業技術學院對比四川汽車職業技術學院哪個好 附分..
時間:2025-05-22 09:08:12成都銀杏酒店管理學院在重慶高考招生計劃人數和專業代..
時間:2025-05-22 09:05:01四川上山東理工大學多少分 分數線及排名
時間:2025-05-22 09:01:18江西高考理科533分排名多少 排名多少位次
時間:2025-05-22 08:57:21大連東軟信息學院對比甘肅民族師范學院哪個好 附分數線..
時間:2025-05-22 08:53:39廣東高考455至460分左右物理可以上什么大學
時間:2025-05-22 08:49:52