📄 subject_33420.htm
字号:
<p>
序号:33420 发表者:胖娃娃 发表日期:2003-03-20 00:35:41
<br>主题:VC6竟然表里不一!!!
<br>内容:诸位看官,小生这里有一段代码(关于模板类),在VB6的IDE环境下,不论DEBUG还是RELEASE模式,都会非法操作错。但是,到系统里DOS窗口中运行,DEBUG模式依旧出同样的错,但是RELEASE模式却一切正常,没有任何错误,而且结果也正确。据我自己跟踪,发现错误发生点在main()函数的最后一句:“return 0”,其实就是执行析构函数时发生了非法操作。跟踪进去到运行库里,我就看不懂了。请问各位过路高手,此因何为?谢过了先。<BR><BR>// TempCls.cpp : Defines the entry point for the console application.<BR>//<BR><BR>#include "stdafx.h"<BR>#include "iostream.h"<BR><BR>template<class _type> class MyTempCls<BR>{<BR> private:<BR> _type *e;<BR> public:<BR> long nCnt;<BR> MyTempCls(const int idxcnt = 16);<BR> MyTempCls(_type ev, const int idxcnt = 16);<BR> ~MyTempCls();<BR> _type& operator[](const int subidx) const;<BR> public:<BR> void SetAt(const int subidx, const _type ev);<BR>};<BR><BR>template<class _type> MyTempCls<_type>::MyTempCls(const int idxcnt)<BR>{<BR> e = new _type[idxcnt];<BR> nCnt = idxcnt;<BR> for(int i = 0; i < nCnt; i++, *e = NULL);<BR>}<BR><BR>template<class _type> MyTempCls<_type>::MyTempCls(_type ev, const int idxcnt)<BR>{<BR> e = new _type[idxcnt];<BR> nCnt = idxcnt;<BR> for(int i = 0; i < nCnt; i++,e[i]=ev);<BR>}<BR><BR>template<class _type> MyTempCls<_type>::~MyTempCls()<BR>{<BR> delete e;<BR>}<BR><BR>template<class _type> _type& MyTempCls<_type>::operator [](const int subidx) const<BR>{<BR> return e[subidx];<BR>}<BR><BR>template<class _type> void MyTempCls<_type>::SetAt(const int subidx, const _type ev)<BR>{<BR> e[subidx] = ev;<BR>}<BR><BR><BR>int main(int argc, char* argv[])<BR>{<BR> MyTempCls<int> nObj(8, 8);<BR> nObj.SetAt(2, 10);<BR><BR> MyTempCls<char> cObj('_', 16);<BR> cObj.SetAt(6, 'B');<BR> cObj.SetAt(8, 'J');<BR> cObj.SetAt(10, 'M');<BR><BR> int i;<BR> for(i = 0; i < nObj.nCnt; i++, cout << nObj[i] << " , ");<BR> cout << endl << endl;<BR> for(i = 0; i < cObj.nCnt; i++, cout << cObj[i]);<BR> cout << endl << endl;<BR> <BR> return 0;<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>回复者:微笑的撒旦 回复日期:2003-03-20 09:09:53
<br>内容:在构造函数中for(int i = 0; i < nCnt; i++,e[i]=ev);有问题。它多占了一个空间并且第一个int地址没有被赋值!e[i]=ev与i++的位置应该交换。<BR><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 + -