×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

Polymorphsim is a good thing, -if you really understand it and know waht you are doing or want to do.

you are not doing "不同派生类转换成基类指针来操".

if say apple is fruit, you want to be able to test :
"a fruit" == "a fruit",
"an apple" == "an apple",
this is perfectly fine, but what you didn't realize is that you also asked:
"an apple" =="a something is fruit"
or
"a something is fruit" == "a something is fruit"
so C++ will ask you to tell it how to do it, you want to compare two apples? or two oranges or one apple and one orange? and HOW?

or: What exactly is your question?
- this apple is/is not that apple?
or
- an orange is/is not an apple?
Report

Replies, comments and Discussions:

  • 工作学习 / 专业技术讨论 / 我要重载基类和派生类的操作符==,直接比较这2个类的instances的成员的值,要保证下面的比较结果都正确:base1 == base2; base1 == (base)derived1; (base)derived1 == derived1; derived1 == base1(出错误提示?)我没想到什么特别好的办法。
    • 没人吱声,是太简单了吗
      • 没看明白. IT? 什么语言?
        • C++
    • 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?
      • 我需要支持强制类型转换后的比较,必须用虚函数,基/派生类函数定义必须一样。具体做法大概跟你这个类似吧。bool base::operator ==(const base &);bool derived::operator==(const base &);derived::operator==()函数里面把输入参数base强制转换成派生类。但是有个问题
        base b1;derived d1;if(d1 == b1)就会有越界错误。其实可以定义一个虚函数返回结构大小,然后根据大小确定是基类还是派生类,我嫌麻烦。
        • 应该是virtual bool base::operator ==(const base &);virtual bool derived::operator==(const base &);
        • 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.
          • 说的就是基类和派生类不同,否则根本就不是问题,基类重载++就可以了。dynamic_cast不错的主意。
        • 定义templeate 函数,然后specify成derived class, 作为对你这种特殊情况的特殊处理。
    • 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 operators
      bool operator==(const Base &, const Derived &);
      bool operator==(const Derived &, const Base &);
      bool operator==(const Derived &, const Derived &);
      • 这个我想过,不用虚函数是不行的。比如说2个派生类instances,类型转换为基类后比较就得不出正确结果。
        • This is a very good example of how bad design can result in ugly implementations...
          • 不同派生类转换成基类指针来操作是oop最重要最基本最常用的特点之一,不能说是bad design吧?
            • Polymorphsim is a good thing, -if you really understand it and know waht you are doing or want to do.
              you are not doing "不同派生类转换成基类指针来操".

              if say apple is fruit, you want to be able to test :
              "a fruit" == "a fruit",
              "an apple" == "an apple",
              this is perfectly fine, but what you didn't realize is that you also asked:
              "an apple" =="a something is fruit"
              or
              "a something is fruit" == "a something is fruit"
              so C++ will ask you to tell it how to do it, you want to compare two apples? or two oranges or one apple and one orange? and HOW?

              or: What exactly is your question?
              - this apple is/is not that apple?
              or
              - an orange is/is not an apple?
              • 你的意思是基类只能和基类比,派生类只能和派生类比?根据多态和封装性,派生类可以转换成基类,你要实现基类的比较,就必然要考虑派生类==派生类,派生类==基类和基类==派生类的情况
                下面的2个比较都是正常的应用
                FRUIT afruit;
                APPLE anApple;
                FRUIT pfruit1 = &afruit;
                FRUIT pfruit2 = &anApple;

                if(pfruit1 == pfruit2); // base == derived
                if(*pfruit2 == *pfruit1); // derived == base
                • 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.
                  • 我不是在说possibility,你没明白我的意思,也有可能我表达的不清楚,不过至少忘情水看明白了。:(
                    • 难道非别在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编译好象不能通过吧。
                      • 看样子我真是没说清楚:有一个基类和一个派生类,各有各的成员变量,现在有2个基类的指针,可能指向基类或者派生类实体,我要重载==比较这2个实体所有的成员变量是不是相等。
                        • 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.
                          • 那你老先生不吝赐教,到底上面那种情况要怎么解决好呢?
                          • 你前面说我给的例子是"基类 vs. 基类",所以我才怀疑你没明白我的意思,其实我是想举"基类指针vs. 基类指针"的例子,而基类指针可能指向基类或者派生类instance,我认为这在c++很常见的,不算bad design。
                            • 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.
                              • type cast确实不太好,所以我问他有没有更好的办法。用global operator是达不到目的.
          • This is really true.