📄 subject_41289.htm
字号:
<p>
序号:41289 发表者:Friendkey 发表日期:2003-05-26 00:29:57
<br>主题:类析构的问题
<br>内容:class CTest<BR>{<BR>public:<BR> CTest(int n){iarr=new int(n);};<BR> ~CTest(){delete [] iarr;};<BR> bool setsize(int n){if(m_anbuf=new int(n))return true; else return false;};<BR> int*iarr;<BR>};<BR>void DoNothing(CTest ct){};<BR>void main()<BR>{<BR>CTest ct(5);<BR>DoNothing(ct);//<--删除此行就不会出错<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>回复者:Neil Gan 回复日期:2003-05-26 03:48:37
<br>内容:change DoNothing(CTest ct) to "void DoNothing(CTest& ct)"<BR><BR>your program calls the copy constructor for ct when you call DoNothing. after DoNothing quits, ct's copy is destructed and the buf in ct is deleted. when main quits, ct is destructed again and the buf is deleted the second time.<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>
回复者:Friendkey 回复日期:2003-05-26 12:33:17
<br>内容:thanks
<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 + -