Distinc derived class C++

Distinc derived class C++

本文关键字:C++ class derived Distinc      更新时间:2023-10-16

我有一个基类和两个派生类。现在我想创建一个基类向量。当我添加新元素时,它会检查新元素的哪个类。但是在C++中,没有像instanceOf这样的函数,所以我必须添加变量type来检查。这是我的代码段。

class Element {
public:
int type;
int getIndex() const {
return index;
}
void setIndex(int index) {
this->index = index;
}
const std::string& getMsg() const {
return msg;
}
void setMsg(const std::string& msg) {
this->msg = msg;
}
std::string toString() {
std::stringstream sbuf;
sbuf << "index: " << index << "n";
sbuf << "Message: " << msg << "n";
return sbuf.str();
}
private:
int index;
std::string msg;
};

两个派生类:

class This: public Element {
public:
This():Element(1){}
~This();
const std::vector<std::string>& getArguments() const {
return arguments;
}
void setArguments(const std::vector<std::string>& arguments) {
this->arguments = arguments;
}
void add(std::string value) {
arguments.push_back(value);
}
private:
std::vector<std::string> arguments;

};
class That: public Element {
public:
That():Element(2){}
~That();
const std::string& getThatVal() const {
return thatVal;
}
void setThatVal(const std::string& thatVal) {
this->thatVal = thatVal;
}
private:
std::string thatVal;

};

在另一个类中,我想创建 Element 数组。

class Visitor {
private:
int numOfThis = 0;
int numOfThat = 0;
std::vector<Element> vecEle;
public:
void add(Element ele) {
vecEle.push_back(ele);
if (ele.type == 1) {
numOfThis++;
} else if (ele.type == 2) {
numOfThat++;
}
}
int getNumOfThat() const {
return numOfThat;
}
int getNumOfThis() const {
return numOfThis;
}
};

我的问题是如何更好地处理这个问题? 这种情况是否有设计模式?谢谢

您可以在向量中存储指针而不是对象,如下所示:

std::vector<Element *> vecEle;

然后,可以使用dynamic_cast来确定哪个派生类是指针的类型。

// dynamic_cast will try to convert pointer of base class to derived class, 
// and return NULL on fail
Element *pElem= vecEle[0]; // you can traverse your vector if needed
if (This *pThis = dynamic_cast<This *>(pElem)) {
// object is This type
} else if (That *pThat = dynamic_cast<That *>(pElem)) {
// object is That type
}

类元素应该是一个基类,其中包含一个名为 即VisitElement( Visitor* visitor).

类访客 与您在代码中描述的类无关,它实际上是作为名为Visitor的设计模式实现的。这意味着它包含重载函数

void Visit(This* thisElem) {thisElem->VisitElement(this)}

void Visit(That* thatElem){thatElem->VisitElement(this)}

此访客类还将包含int counterThisint counterThat。在类中,VisitElement 的实现非常简单:

VisitElement( Visitor* visitor){ visitor->counterThis++;}

在课堂上 访问埃莱姆特的实现就像

VisitElement (Visitor* visitor){ visitor->counterThat++;}

最后,为了实际计数,您需要遍历基类 Element 指针的向量以及向量调用visitor->Visit(elemFromBector)中包含的每个指针。循环完成后,您可以在访问者对象中查询所需的两个整数。 请记住,此解决方案涉及多态性和访问者设计模式以及一些您在原始代码中似乎遗漏的封装原则。

不要使用dynamic_cast它是无辜的,它不应该出现在任何生产代码中。如果你认为你需要再想一想dynamic_cast因为你的代码有问题。 最好的建议是阅读多态性封装访问者设计模式,您应该能够非常容易地理解我的逻辑。