This topic has been archived. It cannot be replied.
-
工作学习 / 专业技术讨论 / 我要重载基类和派生类的操作符==,直接比较这2个类的instances的成员的值,要保证下面的比较结果都正确:base1 == base2; base1 == (base)derived1; (base)derived1 == derived1; derived1 == base1(出错误提示?)我没想到什么特别好的办法。
-baalinca(scarb);
2007-5-1
(#3647584@0)
-
没人吱声,是太简单了吗
-baalinca(scarb);
2007-5-1
(#3647633@0)
-
没看明白. IT? 什么语言?
-iloveflowers(新园丁);
2007-5-1
(#3647884@0)
-
C++
-wangqingshui(忘情水);
2007-5-1
(#3647956@0)
-
The first 3 comprisons use bool Base::operator==(const Base &) const; and the last one use bool Derived::operator==(const Base &) const; Have you defined them?
-wangqingshui(忘情水);
2007-5-1
(#3647971@0)
-
我需要支持强制类型转换后的比较,必须用虚函数,基/派生类函数定义必须一样。具体做法大概跟你这个类似吧。bool base::operator ==(const base &);bool derived::operator==(const base &);derived::operator==()函数里面把输入参数base强制转换成派生类。但是有个问题base b1;derived d1;if(d1 == b1)就会有越界错误。其实可以定义一个虚函数返回结构大小,然后根据大小确定是基类还是派生类,我嫌麻烦。
-baalinca(scarb);
2007-5-2
{128}
(#3648675@0)
-
应该是virtual bool base::operator ==(const base &);virtual bool derived::operator==(const base &);
-baalinca(scarb);
2007-5-2
(#3648702@0)
-
It depends on how method bool derived::operator==(const base &); is implemented. If derived class doesn't have more attributes, it cans simply call base class' implementation.If derived class has more attributes, you need to try to cast parameter to derived class. I suppose you use dynamic_cast instead of C style cast. Then you can determine if it's an instance of base class or derived class. Only it's instance of derived class, you continue compare more attributes. Oterwise, the result is false.
-wangqingshui(忘情水);
2007-5-3
{328}
(#3652297@0)
-
说的就是基类和派生类不同,否则根本就不是问题,基类重载++就可以了。dynamic_cast不错的主意。
-baalinca(scarb);
2007-5-4
(#3652841@0)
-
定义templeate 函数,然后specify成derived class, 作为对你这种特殊情况的特殊处理。
-frankwoo(柳五随风);
2007-5-4
(#3655040@0)
-
If you define global operator bool operator==(const Base &, const Base &); implicit cast happens and all of the 4 comparisons can pass compiling. If derived class has more attributes, you may need to define more operatorsbool operator==(const Base &, const Derived &);
bool operator==(const Derived &, const Base &);
bool operator==(const Derived &, const Derived &);
-wangqingshui(忘情水);
2007-5-1
{148}
(#3647986@0)
-
这个我想过,不用虚函数是不行的。比如说2个派生类instances,类型转换为基类后比较就得不出正确结果。
-baalinca(scarb);
2007-5-2
(#3648692@0)
-
This is a very good example of how bad design can result in ugly implementations...
-holx(Dicom);
2007-5-3
(#3651028@0)
-
不同派生类转换成基类指针来操作是oop最重要最基本最常用的特点之一,不能说是bad design吧?
-baalinca(scarb);
2007-5-3
(#3651301@0)
-
Polymorphsim is a good thing, -if you really understand it and know waht you are doing or want to do.
-holx(Dicom);
2007-5-3
{589}
(#3652033@0)
-
你的意思是基类只能和基类比,派生类只能和派生类比?根据多态和封装性,派生类可以转换成基类,你要实现基类的比较,就必然要考虑派生类==派生类,派生类==基类和基类==派生类的情况下面的2个比较都是正常的应用
FRUIT afruit;
APPLE anApple;
FRUIT pfruit1 = &afruit;
FRUIT pfruit2 = &anApple;
if(pfruit1 == pfruit2); // base == derived
if(*pfruit2 == *pfruit1); // derived == base
-baalinca(scarb);
2007-5-3
{204}
(#3652170@0)
-
I'm not talking about posibility, I'm talking about the bad design. And by the way :-),;-), the examples you give me are "基类 vs. 基类", try another one please.
-holx(Dicom);
2007-5-3
{76}
(#3652267@0)
-
我不是在说possibility,你没明白我的意思,也有可能我表达的不清楚,不过至少忘情水看明白了。:(
-baalinca(scarb);
2007-5-4
(#3652826@0)
-
难道非别在base class 和derived-class里面overload operator ==不能满足你的需求?class Base
{
public:
bool operator == ( const Base & a ) { ... }
};
class Derived : public Base
{
public:
bool operator == ( const Derived& d )
{
if( ! Base::operator == ( d ) )
return false;
.....
}
};
针对你的问题, 1, 2两项对应basse class 的==, 3对应derived class的==,4编译好象不能通过吧。
-frankwoo(柳五随风);
2007-5-4
{433}
(#3655019@0)
-
看样子我真是没说清楚:有一个基类和一个派生类,各有各的成员变量,现在有2个基类的指针,可能指向基类或者派生类实体,我要重载==比较这2个实体所有的成员变量是不是相等。
-baalinca(scarb);
2007-5-5
(#3655400@0)
-
It's not because you "真是没说清楚", we all here are crystal "清楚", except you. :-). Please don`t take anything personal, just want to let you know the truth, it’s not a C++ problem,you are still in very early stage of learning OO, and because of your poor understanding of the OO, you are proposing a very bad design, which makes no sense. However is perfectly normal for beginners.
-holx(Dicom);
2007-5-6
{201}
(#3657029@0)
-
那你老先生不吝赐教,到底上面那种情况要怎么解决好呢?
-baalinca(scarb);
2007-5-6
(#3658113@0)
-
你前面说我给的例子是"基类 vs. 基类",所以我才怀疑你没明白我的意思,其实我是想举"基类指针vs. 基类指针"的例子,而基类指针可能指向基类或者派生类instance,我认为这在c++很常见的,不算bad design。
-baalinca(scarb);
2007-5-6
(#3658158@0)
-
type check or cast in overriden methods in derived class is actually kind of bad design. So for your case, global operator is better than virtual operator.
-wangqingshui(忘情水);
2007-5-6
(#3658261@0)
-
type cast确实不太好,所以我问他有没有更好的办法。用global operator是达不到目的.
-baalinca(scarb);
2007-5-6
(#3658321@0)
-
This is really true.
-wangqingshui(忘情水);
2007-5-3
(#3652298@0)