您可以创建继承类的 std::map 吗?
我想知道是否可以创建继承类的指针映射。这是我想要做的一个示例:
#include <string>
#include <map>
using namespace std;
class BaseClass
{
string s;
};
class Derived1 : public BaseClass
{
int i;
};
class Derived2 : public Derived1
{
float f;
};
// Here's what I was trying, but isn't working
template<class myClass>
map<string, myClass>m;
int main()
{
// Add BaseClasses, Derived1's, and/or Derived2's to m here
return 0;
}
我得到的错误是:
main.cpp(23): error C2133: 'm' : unknown size main.cpp(23): error C2998: 'std::map<std::string,myClass>m' : cannot be a template definition
我明白为什么会收到此错误,但我想知道是否可以创建一个可以保存不同级别的继承类的映射?如果没有,是否可以创建某种可以容纳各种类别类型的管理系统?或者我必须制作不同的地图/向量/数组/等。对于每种类型的课程?
I'm wondering if it's possible to create a map of pointers of inherited classes. Here's an example of what I'm trying to do:
#include <string>
#include <map>
using namespace std;
class BaseClass
{
string s;
};
class Derived1 : public BaseClass
{
int i;
};
class Derived2 : public Derived1
{
float f;
};
// Here's what I was trying, but isn't working
template<class myClass>
map<string, myClass>m;
int main()
{
// Add BaseClasses, Derived1's, and/or Derived2's to m here
return 0;
}
The errors I get are:
main.cpp(23): error C2133: 'm' : unknown size main.cpp(23): error C2998: 'std::map<std::string,myClass>m' : cannot be a template definition
I get why I'm getting this error, but I'm wondering if it's possible to create a map that can hold different levels of inherited classes? If not, is it possible to create some sort of management system that can hold various class types? Or would I have to make different maps/vectors/arrays/etc. for each type of class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,您可以在映射中存储继承的类,但存储指向它们的指针,而不是对象本身。这是一个简短的示例(它缺乏指针的内存管理)
Yes you can store inherited classes in map, but pointers to them, not objects themselves. Here's a short example (it lacks memory management on pointers)
首先要做的事情是:
这不是有效的 C++,只能意味着类似模板别名的东西,但我相信,这不是您正在寻找的东西。
在 C++ 中存储多态对象由于切片(从派生类型的值构造基类型的值)而变得复杂。动态多态只能通过引用或指针来处理。对于映射仅在调用堆栈中传递的情况,您可以使用。自己管理释放是相当乏味的,或者
std::ref
或boost::ref
,但这需要小心。通常,存储指向基址的指针是可行的方法:std::mapstd::map
或std::map< /code> 是首选,具体取决于您是否需要共享语义。
基本示例。有人应该用更有意义的东西来取代它。
First things first:
This is not valid C++ and could only mean something like a
template alias
, but I believe, that is not what you are looking for.Storing polymorphic objects in C++ is complicated by slicing (constructing a value of the base type from a value of a derived type). Dynamic polymorphism can only be handled through references or pointers. You could potentially use
std::ref
orboost::ref
for situations in which the map will only be passed down the callstack, but this requires some care. Often, storing pointers to the base is the way to go:std::map<std::string, base*>
. Managing deallocation yourself is rather tedious and eitherstd::map<std::string, std::unique_ptr>
orstd::map<std::string, std::shared_ptr>
are preferred, depending if you need shared semantics or not.Basic example. Someone should replace this with something more meaningful.
您可能在类和函数上有模板,但在实例上没有。
您应该坚持使用
BaseClass*
的映射。You may have template on classes and functions, but not on instances.
You should stick to the map to
BaseClass*
'es.下面是anton建议的解决方案的扩展。
Below is the expansion of solution suggested by anton.