📄 subject_40099.htm
字号:
<p>
序号:40099 发表者:heavenkiller 发表日期:2003-05-15 17:55:41
<br>主题:关于Object基类重载操作符时出现的重大问题!!!
<br>内容:Object.h<BR><BR>#include <iostream.h><BR>class Object<BR>{<BR> friend ostream& operator <<(ostream& , Object const& );<BR> <BR><BR>public:<BR><BR><BR> virtual int CompareTo (Object const&) const = 0;<BR><BR>//<BR> virtual ~Object ();<BR> virtual bool IsNull () const;<BR> virtual int Compare (Object const&) const;<BR><BR> //virtual ostream& operator <<(ostream& , Object const& );<BR> //virtual ostream& operator <<(ostream& );<BR><BR> <BR> virtual void Put (ostream&) const = 0;<BR> <BR><BR>//在重载时注意:<BR>// != 0;<BR> // = 1;<BR> // < -10;<BR> // > 10;<BR> //两个操作符加起来时<BR> //<= -11;<BR> //>= 11<BR><BR> //<BR>inline bool operator == ( Object const& right)<BR> { return (*this).Compare (right) == 1; }<BR><BR>inline bool operator != ( Object const& right)<BR> { return (*this).Compare (right) == 0; }<BR><BR>inline bool operator <= ( Object const& right)<BR> { return (*this).Compare (right) == -11; }<BR><BR>inline bool operator < ( Object const& right)<BR> { return (*this).Compare (right) == -10; }<BR><BR>inline bool operator >= ( Object const& right)<BR> { return (*this).Compare (right) == 11; }<BR><BR>inline bool operator > ( Object const& right)<BR> { return (*this).Compare (right) == 10; }<BR><BR><BR><BR><BR>};<BR><BR><BR><BR>object.cpp<BR><BR><BR>#include "Object.h"<BR><BR>#include <typeinfo><BR><BR>Object::~Object ()<BR> {}<BR><BR>bool Object::IsNull () const<BR> { return false; }<BR><BR>int Object::Compare (Object const& object) const<BR>{<BR> if (typeid (*this) == typeid (object))<BR> return CompareTo (object);<BR> else if (typeid (*this).before (typeid (object)))<BR> return -1;<BR> else<BR> return 1;<BR>}<BR><BR><BR>ostream& operator <<(ostream& s,Object const & object)<BR> { object.Put (s); return s; }<BR><BR>SomeObject.h<BR><BR>#include "Object.h"<BR>class SomeObject :public Object<BR>{<BR><BR>public:<BR> int i;<BR>public:<BR><BR><BR> int CompareTo(Object const&) const;<BR><BR><BR> SomeObject(int=0);<BR> virtual ~SomeObject();<BR><BR> //pure virtual function<BR> void Put(ostream &) const;<BR> <BR>};<BR><BR>SomeObject.cpp<BR><BR>#include "SomeObject.h"<BR><BR>//////////////////////////////////////////////////////////////////////<BR>// Construction/Destruction<BR>//////////////////////////////////////////////////////////////////////<BR><BR>SomeObject::SomeObject(int object)<BR>{<BR> i=object;<BR>}<BR><BR>SomeObject::~SomeObject()<BR>{<BR><BR>}<BR><BR>void SomeObject::Put(ostream & s)const<BR>{<BR> s<<i;<BR>}<BR><BR>int SomeObject::CompareTo(Object const& some)const<BR>{<BR> SomeObject const & someobject=dynamic_cast<SomeObject const &>(some)<BR>//////////////////////////////////////////////////////////////// <BR> if (i==(someobject.i)) ///***出错的地方<BR>/////////////////////////////////////////// <BR> return 1; <BR><BR> if (i!=someobject.i)<BR> return 0;<BR><BR><BR>if (i<=someobject.i)<BR>return -11;<BR><BR>if (i<someobject.i)<BR>return -10;<BR>if (i>=someobject.i)<BR>return 11;<BR>if (i>someobject.i)<BR>return 10;<BR><BR>}<BR><BR><BR>debug.cpp<BR><BR>#include "SomeObject.h"<BR>#include "iostream.h"<BR><BR><BR>main()<BR>{<BR> char z;<BR> int inta=10;<BR> char chara='a';<BR><BR> SomeObject a(1);<BR> SomeObject b(2);<BR><BR> cout<<a<<" "<<b<<endl;<BR> cout<<"a.i"<<a.i<<endl;<BR>///////////////////////////////////////////<BR> if (a==b)<BR> cout<<"the bigger is:"<<a;<BR> else cout <<"the bigger is:"<<b;<BR>////////////////////////////////////////////<BR><BR> cin >>z;<BR> <BR> return 0;<BR>}<BR><BR>这是我编的一个Object基类,以及其子类SomeObject类,我重载了==,<=等操作符,但是当我将<BR><BR><BR>///////////////////////////////////////////<BR> if (a==b)<BR> cout<<"the bigger is:"<<a;<BR> else cout <<"the bigger is:"<<b;<BR>////////////////////////////////////////////<BR>代码加入到main()中的时候,调试出现错误,弹出异常。<BR>异常指示如上所示,在<BR>//////////////////////////////////////////////////////////////// <BR> if (i==(someobject.i)) ///***出错的地方<BR>///////////////////////////////////////////<BR>处,<BR>可是这里有什么错误?<BR>请指示一下!<BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><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>
回复者:heavenkiller 回复日期:2003-05-27 20:30:55
<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>
<font color=red>答案被接受</font><br>回复者:哎哟 回复日期:2003-05-29 08:01:40
<br>内容:1.你这段代码虽然写的很别扭,但是至少在你说出错的地方,应该是对的。你用的什么编译器?RTTI打开了吗?<BR>2.不要用iostream.h,用iostream<BR>3.你的代码就够非常混乱。<BR>4.friend ostream& operator <<(ostream& , Object const& );这种实现方法不好。应该用member+func法,可以省去一个难看的friend。参见<BR>http://www.allaboutprogram.com/bb/viewtopic.php?t=25&postdays=0&postorder=asc&start=0
<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 + -