📄 subject_29812.htm
字号:
<p>
序号:29812 发表者:晨风 发表日期:2003-02-13 19:13:27
<br>主题:关于CList释放内存的问题!!
<br>内容:见下面程序:<BR>class A<BR>{<BR> int* mem;<BR> int n;<BR>public:<BR> A()<BR> {<BR> n=0;<BR> mem=NULL;<BR> }<BR> A(int number)<BR> {<BR> n=number;<BR> mem=new int[n];<BR> memset(mem,0,sizeof(int)*number);<BR> }<BR> A(const A& A1)<BR> {<BR> n=A1.n;<BR> mem=new int[n];<BR> memset(mem,0,n);<BR> }<BR> ~A()<BR> {<BR> delete []mem;<BR> mem=NULL;<BR> }<BR>};<BR><BR>main()<BR>{<BR> A t1(2);<BR> CList<A,A&> list;<BR> list.AddTail(t1);<BR> list.RemoveAll();<BR><BR> return 0;<BR>}<BR><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>回复者:mr_oydy 回复日期:2003-02-13 22:24:36
<br>内容:A& operator=(const A& other){<BR> if(&other==this)<BR> return *this;<BR> delete[] mem;<BR> mem = new int[other.n];<BR> memcpy(mem, other.mem, n);<BR> return *this;<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>
回复者:mr_oydy 回复日期:2003-02-13 22:27:38
<br>内容:/* afxtempl.h */<BR>template<class TYPE, class ARG_TYPE><BR>POSITION CList<TYPE, ARG_TYPE>::AddTail(ARG_TYPE newElement)<BR>{<BR> ASSERT_VALID(this);<BR><BR> CNode* pNewNode = NewNode(m_pNodeTail, NULL);<BR> pNewNode->data = newElement;<BR>....<BR>...<BR>}<BR><BR>// pNewNode->data = newElement; (赋值操作)
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:未出手的飞刀 回复日期:2003-02-13 23:33:51
<br>内容:<BR>先new出一块内存然后构造再赋值,从中也见mfc的容器设计确有不足之处。<BR><BR>若用stl就没类似问题<BR>A t1(2);<BR>std::list<A> lst ;<BR><BR>lst.push_back(t1) ;<BR>lst.clear() ;
<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 + -