Virtual function implementation C++ not working
我是C++新手,我尝试编写一个简单代码来比较父类的两个类的类,称为可比较的类。我希望每个子类都有自己的方法实现,以便根据它们所保存的数据比较对象,因此我使用了虚关键字:
1 2 3 4 | class Comparable { public: virtual int compare(Comparable *other); }; |
例如,我的子类highScoreElement将有自己的compare实现,将对象的得分与另一个highScoreElement的得分进行比较。
以下是我的子类HighScoreElement:
1 2 3 4 5 6 7 8 9 | 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中的比较实现中,我首先尝试检查当前对象的数据是否与其他对象的数据相同。但是由于指向其他的指针是类可比的,而不是高分的,所以在我的代码中我根本不能引用其他的->分数,即使高分是可比的子类。
以下是迄今为止的完整代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #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... } } |
当我编写此代码时,会立即得到一个编译器错误:
1 | if (this->score == other->score) |
。
因为另一个没有被称为score的数据,但是它的子类highScoreElement有。如何修复我的函数实现,以便引用"其他"的数据?我知道我的问题听起来可能很含糊,但任何帮助都会得到感激!
您可以实现一个虚拟函数getScore(),可能是基类中的纯虚拟函数,并使用它来代替访问比较函数中的字段得分。使其成为常量方法。另一方面,比较可以是在基类中实现的一种方法,它使用
代码存根:
1 2 3 4 5 6 7 8 9 10 11 | 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;} } |
如果准备接受空指针,则可以使用动态强制转换。当您比较
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #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) { ; } } |
可以使用"dynamic"强制转换传递给highScoreElement::compare的指针(失败时会引发错误的强制转换异常)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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... } } |
你确定不是吗
比较(其他比较)
if(this->score==other.score)