您可以const static std::string TYPE
在每个子类上拥有一个成员(例如,使用一个包含子类名称的字符串)和一个virtual std::string getType()
返回该 TYPE 成员的方法。这样你就可以比较指针返回的值:
ThinkFunc.h
class ThinkFunc{
public:
virtual const std::string& getType();
};
GroupBoxBouncePan.h
namespace Thinkers{
class GroupBoxBouncePan{
public:
static const std::string TYPE;
virtual const std::string& getType(); // returns TYPE
};
};
GroupBoxBouncePan.cpp
const std::string Thinkers::GroupBoxBouncePan::TYPE = "Thinkers::GroupBoxBouncePan";
const std::string& Thinkers::GroupBoxBouncePan::getType(){
return TYPE;
}
搜索将是这样的:
bool found = false;
ThinkFunc *tfNode;
for (tfNode = this->thinkfuncs; tfNode; tfNode = (ThinkFunc*)tfNode->next)
{
if (tfNode->getType() == Thinkers::GroupBoxBouncePan::TYPE)
{
found = true;
break;
}
}