📄 subject_17386.htm
字号:
<p>
序号:17386 发表者:gogo 发表日期:2002-10-11 07:26:24
<br>主题:高分请教一道考题!
<br>内容:class String<BR>{<BR> public:<BR> String(const char *str=NULL); //普通构造函数<BR> String(const String &other); //拷贝构造函数<BR> ~String(void); //析构函数<BR> String &operate(const string &other) //赋值函数<BR> <BR> private:<BR> char *m_date //用来保存字符串<BR>}<BR>任写其中三个函数 <BR><BR>急!可以令开贴子给分!
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:iwill 回复日期:2002-10-11 08:09:51
<br>内容:瞎写的:<BR><BR>String::String(const char *str)<BR>{<BR> if (str)<BR> {<BR> m_data = new char[strlen(str) + 1];<BR> strcpy(m_data, str);<BR> }<BR> else <BR> {<BR> m_data= new char[1];<BR> *m_data =0;<BR> }<BR>}<BR><BR><BR>String::String(const String &other)<BR>{<BR> m_data=new char[strlen(other.m_data)+1];<BR> strcpy(m_data,other.m_data);<BR><BR>}<BR><BR>String& String::operator= (const String &other) <BR>{<BR><BR> m_data=new char[strlen(other.m_data)+1];<BR> strcpy(m_data,other.m_data);<BR> return *this;<BR><BR>}<BR><BR>String::~String()<BR>{<BR> delete []m_data;<BR> m_data=NULL;<BR>}<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:黄飚 回复日期:2002-10-11 08:38:36
<br>内容:标准答案:<BR><BR>// String的析构函数<BR> String::~String(void) // 3分<BR>{<BR> delete [] m_data; <BR>// 由于m_data是内部数据类型,也可以写成 delete m_data;<BR> }<BR><BR> // String的普通构造函数 <BR> String::String(const char *str) // 6分<BR>{<BR> if(str==NULL) <BR> {<BR> m_data = new char[1]; // 若能加 NULL 判断则更好<BR> *m_data = ‘\0’; <BR> } <BR> else<BR> {<BR> int length = strlen(str); <BR> m_data = new char[length+1]; // 若能加 NULL 判断则更好 <BR> strcpy(m_data, str); <BR> }<BR>} <BR>// 拷贝构造函数<BR> String::String(const String &other) // 3分<BR> { <BR> int length = strlen(other.m_data); <BR> m_data = new char[length+1]; // 若能加 NULL 判断则更好 <BR> strcpy(m_data, other.m_data); <BR>}<BR>// 赋值函数<BR> String & String::operate =(const String &other) // 13分<BR> { <BR> // (1) 检查自赋值 // 4分<BR> if(this == &other)<BR> return *this;<BR> <BR>// (2) 释放原有的内存资源 // 3分<BR> delete [] m_data;<BR> <BR> // (3)分配新的内存资源,并复制内容 // 3分<BR> int length = strlen(other.m_data); <BR> m_data = new char[length+1]; // 若能加 NULL 判断则更好<BR> strcpy(m_data, other.m_data);<BR> <BR> // (4)返回本对象的引用 // 3分<BR> return *this;<BR>} <BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:凯凯 回复日期:2002-10-11 08:41:03
<br>内容:摘自林锐的<高质量C++/C编程指南><BR><BR><BR>// String的析构函数<BR>String::~String(void) <BR>{<BR> delete [] m_data; <BR>// 由于m_data是内部数据类型,也可以写成 delete m_data;<BR> }<BR><BR> // String的普通构造函数 <BR> String::String(const char *str) <BR>{<BR> if(str==NULL) <BR> {<BR> m_data = new char[1]; // 若能加 NULL 判断则更好<BR> *m_data = ‘\0’; <BR> } <BR> else<BR> {<BR> int length = strlen(str); <BR> m_data = new char[length+1]; // 若能加 NULL 判断则更好 <BR> strcpy(m_data, str); <BR> }<BR>} <BR>// 拷贝构造函数<BR> String::String(const String &other) <BR> { <BR> int length = strlen(other.m_data); <BR> m_data = new char[length+1]; // 若能加 NULL 判断则更好 <BR> strcpy(m_data, other.m_data); <BR>}<BR>// 赋值函数<BR> String & String::operate =(const String &other) <BR> { <BR> // (1) 检查自赋值 <BR> if(this == &other)<BR> return *this;<BR> <BR>// (2) 释放原有的内存资源 <BR> delete [] m_data;<BR> <BR> // (3)分配新的内存资源,并复制内容 <BR> int length = strlen(other.m_data); <BR> m_data = new char[length+1]; // 若能加 NULL 判断则更好<BR> strcpy(m_data, other.m_data);<BR> <BR> // (4)返回本对象的引用 <BR> return *this;<BR>}<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:iwill 回复日期:2002-10-11 08:43:17
<br>内容:林锐的习题答案 呵呵
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:凯凯 回复日期:2002-10-11 08:43:32
<br>内容:晚了一步。<BR><BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:凯凯 回复日期:2002-10-11 08:46:59
<br>内容:其实真要想研究可以看看MFC中CString类的源码。在strex.cpp文件中。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:gogo 回复日期:2002-10-11 08:52:02
<br>内容:pankai,你的头像好酷哦,不过还是觉得原来得更酷 ,哈哈<BR>都谢谢各位!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -