[英]store pointers to different types of classes in an std::unordered_map
I have devices I need to do operations on. 我有需要对其进行操作的设备。 I have one base class
BASE
and a child class TYPE1
. 我有一个基类
BASE
和一个子类TYPE1
。 Each device is an instance of TYPE1
. 每个设备都是
TYPE1
一个实例。 These get instantiated by another class depending on what is present in an xml configuration file. 这些由另一个类实例化,具体取决于xml配置文件中的内容。
class BASE {
public:
BASE();
virtual ~BASE();
};
class TYPE1 : public BASE {
public:
TYPE1();
};
Now I store pointers to these instances in an std::unordered_map
defined as: 现在,我将指向这些实例的指针存储在
std::unordered_map
定义为:
std::unordered_map <std::string, TYPE1 *> myDevices;
with std::string
being an identification key that is also used in configuration files. 其中
std::string
是标识密钥,还用于配置文件中。
The std::unordered_map
gives me quick direct access to an object if I need it and the convenience to do the same operation on all the devices if I iterate through it using 如果需要,
std::unordered_map
让我快速直接访问对象,如果使用进行迭代,则可以方便地在所有设备上执行相同的操作
for ( auto& elem : myDevices ) {
//do stuff
}
The order of the devices is unimportant, hence the std::unordered_map
. 设备的顺序并不重要,因此
std::unordered_map
。 I use that extensively throughout the code. 我在整个代码中广泛使用了该代码。
Now I have the need for a TYPE2
that is also a child of BASE
but nevertheless a different type. 现在,我需要一个
TYPE2
,它也是BASE
的子代,但是仍然是不同的类型。
TYPE1
and TYPE2
both have the same methods implemented - they function differently - but yield the same results TYPE1
和TYPE2
都实现了相同的方法-功能不同-但产生的结果相同
My question : 我的问题 :
How do I modify 我该如何修改
std::unordered_map <std::string, TYPE1 *> myDevices;
so it accepts all types of classes like 因此它接受所有类型的类,例如
std::unordered_map <std::string, acceptAnyTypeOfClass *> myDevices;
I'm blocked with this and while I think I could get around it it would get ugly very quickly and I would really like to do it in a clean way. 我对此感到困惑,虽然我认为我可以解决它,但是它很快就会变得很丑陋,我真的很想以一种干净的方式来做。 Is what I'm asking possible and if so how please?
我要问的可能吗?如果可以,请问如何?
The first option that comes to mind would be to use a pointer to the base class, because TYPE1
and TYPE2
objects are all BASE
objects: 首先想到的是使用指向基类的指针,因为
TYPE1
和TYPE2
对象都是BASE
对象:
std::unordered_map <std::string, BASE*> myDevices;
The question is then ho wto make the difference between TYPE1*
pointers and TYPE2*
pointers ? 那么问题是如何使
TYPE1*
指针和TYPE2*
指针之间有区别?
As TYPE1
and TYPE2
both have the same methods implemented, the best approach would probably be to use polymorphism, using virtual functions: 由于
TYPE1
和TYPE2
都实现了相同的方法,因此最好的方法可能是通过虚拟函数使用多态:
class BASE {
public:
BASE();
virtual void doSomething()=0;
virtual ~BASE();
};
class TYPE1 : public BASE {
public:
TYPE1();
void doSometing() override { /* the code for TYPE 1 devices*/ }
};
class TYPE2 : public BASE {
public:
TYPE2();
void doSometing() override { /* the code for TYPE 2 devices*/ }
};
You can then invoke the functions without worrying about the true type of the base object: 然后,您可以调用函数而不必担心基础对象的真实类型:
std::unordered_map <std::string, BASE*> myDevices;
...
for (auto& elem : myDevices ) {
elem->doSomething();
}
PS: If you create the device objects dynamically, you could consider using unique or shared pointers in the map, to avoid memory leaks. PS:如果动态创建设备对象,则可以考虑在映射中使用唯一或共享的指针,以避免内存泄漏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.