3

我是 C++ 新手,我正在尝试编写一个简单的代码来比较名为 Comparable 的父类的子类的两个对象。我希望每个子类都有自己的方法实现,以便根据对象持有的数据来比较对象,所以我使用了 virtual 关键字:

class Comparable {  
public:
virtual int compare(Comparable *other); 
    };

例如,我的子类 HighScoreElement 将有自己的比较实现,它将对象的分数与另一个 HighScoreElement 的分数进行比较。

这是我的子类 HighScoreElement:

class HighScoreElement: public Comparable { 
        public: 
virtual int compare(Comparable *other); 
HighScoreElement(string user_name, int user_score); // A constructor
        private: 
int score; 
string name;

 };

但是在我在 HighScoreElement 中的比较实现中,我首先尝试检查当前对象的数据是否与其他对象的数据相同。但是由于指向 other 的指针属于 Comparable 类而不是 HighScoreElement,因此我的代码中根本无法引用 other->score,即使 HighScoreElement 是 Comparable 的子类。

这是到目前为止的完整代码:

#include <iostream> 
using namespace std; 

class Comparable {
public: 
virtual int compare(Comparable *other);
    };

class HighScoreElement: public Comparable {
public: 
    virtual int compare(Comparable *other);
    HighScoreElement(int user_score, string user_name);
private:
    string name;
    int score; 
};

HighScoreElement::HighScoreElement(int user_score, string user_name) {
name = user_name; 
score = user_score; 
}



int HighScoreElement::compare(Comparable *other) {
if (this->score == other->score) { // Compiler error right here, other->score is invalid.
    // Code to do the comparing if two scores are equal...
}
}

编写此代码时,我立即收到编译器错误:

if (this->score == other->score)

因为 other 没有称为 score 的数据,但它的子类 HighScoreElement 有。如何修复我的函数实现,以便我可以引用“其他”的数据?我知道我的问题可能听起来含糊不清,但任何帮助将不胜感激!

4

4 回答 4

1

您可以在基类中实现一个虚函数 GetScore(),可能是纯虚函数,并使用它而不是访问比较函数中的字段分数。使其成为 const 方法。另一方面,Compare 可以是在基类中实现的方法,它使用this->GetScore()other->GetScore()

代码存根:

class A {
   virtual int getScore() const = 0;
   inline bool compare(const A* in) {return (in && this->getScore() == in->getScore());}
   //return false also if "in" is set to NULL
   }


class B : public A {
   int score;
   inline int getScore() const {return score;}
   }
于 2013-07-19T22:03:42.483 回答
0

您可以使用“dynamic_cast”强制转换传递给 HighScoreElement::compare 的指针(失败时会抛出 bad_cast 异常)。

int HighScoreElement::compare(Comparable *other) {
HighScoreElement *h = NULL; 
try
{
    ptr = dynamic_cast<HighScoreElement *>(other); 
}
catch(std::bad_cast const &)
{
    // Handle the bad cast...
}
if (this->score == ptr->score) { 
// Code to do the comparing if two scores are equal...
}
}
于 2013-07-19T22:19:32.630 回答
0

如果您准备接受空指针,则可以使用动态强制转换。当您比较HighScoreElement指针以避免不必要的强制转换时,您可能会对这种情况进行重载。

#include <iostream> 
using namespace std; 

class Comparable {
public: 
  virtual int compare(Comparable *other) = 0;  // made pure virtual to compile without definition
};

class HighScoreElement: public Comparable {
public: 
  virtual int compare(Comparable *other);
  int compare(HighScoreElement *other); // comparing to a HighScoreElement ptr, no need to dynamic cast
  HighScoreElement(int user_score, string user_name);
private:
  string name;
  int score; 
};

HighScoreElement::HighScoreElement(int user_score, string user_name) {
  name = user_name; 
  score = user_score; 
}

int HighScoreElement::compare(Comparable *other) {
  HighScoreElement * pHSE = dynamic_cast<HighScoreElement*>(other);
  if (pHSE) {
    return compare(pHSE);
  } else {
    return -1; // or however you want to handle compare to non HighScoreElement
  }
}

int HighScoreElement::compare(HighScoreElement *other) {
  if (this->score == other->score) {
    ;
  }
}
于 2013-07-19T22:25:01.020 回答
-1

你确定不是

比较(比较其他

如果 (this->score == other.score )

于 2013-07-19T22:09:38.423 回答