Distinc derived class C++
Distinc derived class C++
我有一个基类和两个派生类。现在我想创建一个基类向量。当我添加新元素时,它会检查新元素的哪个类。但是在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 counterThis
和int counterThat
。在类中,VisitElement 的实现非常简单:
VisitElement( Visitor* visitor){ visitor->counterThis++;}
在课堂上 访问埃莱姆特的实现就像
VisitElement (Visitor* visitor){ visitor->counterThat++;}
最后,为了实际计数,您需要遍历基类 Element 指针的向量以及向量调用visitor->Visit(elemFromBector)
中包含的每个指针。循环完成后,您可以在访问者对象中查询所需的两个整数。 请记住,此解决方案涉及多态性和访问者设计模式以及一些您在原始代码中似乎遗漏的封装原则。
不要使用dynamic_cast它是无辜的,它不应该出现在任何生产代码中。如果你认为你需要再想一想dynamic_cast因为你的代码有问题。 最好的建议是阅读多态性,封装和访问者设计模式,您应该能够非常容易地理解我的逻辑。
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- "Undefined class"作为is_base_of的论据
- 如何将class.cpp和class.hpp编译为一个.o文件
- '{'标记之前的预期类名,然后在预声明时无效使用不完整的类型'class class_name'
- 如何使用"derived input class"创建派生类?
- 错误:"Left of getValue must have class/struct/union"
- ' class a : b ' 和 ' class a : public b ' 之间的继承类不同
- Pybind11 Class Definition
- 在C++中使用没有合作伙伴Class/Cpp文件的头文件是否实用
- C2011 'CMemDC':Visual Studio 2019中的'class'类型重新定义
- TypeError: [c++ addon class] 不是构造函数
- C++ class template
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- Clearing Class Foo with new(pFoo) Foo()
- C++ class vs a library
- 继承类时"invalid use of incomplete type ‘class tree_node_t’"
- 使用"class"关键字,后跟未声明的标识符
- VSCode - C++对"CLASS::FUNCTION"的未定义引用