我是C ++的新手,我正在尝试编写一个简单的代码来比较一个名为Comparable的父类的子类的两个对象。我希望每个子类都有自己的方法实现来根据它们持有的数据比较对象,所以我使用了虚拟关键字:
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的比较实现中,我首先尝试检查当前对象的数据是否与其他数据相同。但由于指向其他的指针属于Comparable类而不是HighScoreElement,我在代码中根本不能引用other->得分,即使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)
因为其他人没有名为score的数据,但其子类HighScoreElement却没有。如何修复我的函数实现,以便我可以引用“其他”的数据?我知道我的问题可能听起来很模糊,但任何帮助都会受到赞赏!
答案 0 :(得分:1)
您可以在基类中实现虚拟函数GetScore(),可能是pure virtual,并使用它而不是在比较函数中访问字段分数。使它成为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;}
}
答案 1 :(得分: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...
}
}
答案 2 :(得分: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) {
;
}
}
答案 3 :(得分:-1)
你确定它不是
比较(可比较的其他)
如果(此 - >分数== other.score )