使用“ unique_ptr”创建``映射''
我最初有一个问题,创建了类的映射
,并在一些帮助下 我意识到我实际上需要一个map< string,unique_ptr< myclass>
。
更确切地说:
- 我有很多专业班级和一个共同的祖先。
- 专业课程实际上是从同一模板中专门的。
- 我有一个
std :: map< std :: string,unique_ptr< ancestor>>
获得所有权 我的实例(我希望)。 - 我需要所有权,因为创建实例的函数在 完全不同的代码部分与使用它们的位置。
- 如果有用:我在初始化时创建
map
,然后我只 在运行时引用它;初始化后出于所有实际目的 它(一个复杂的事件,涉及读取配置文件)它可能成为const
。
我需要实现的最小示例是:
#include <iostream>
#include <string>
#include <memory>
#include <map>
class generic {
std::string _name;
public:
generic(std::string name) : _name(name) {}
virtual ~generic() = default;
virtual std::string name() { return _name; }
virtual std::string value() { return "no value in generic"; }
};
template <class T> class special : public generic {
T _value;
public:
special(std::string name, T value) : generic(name), _value(value) {}
virtual ~special() = default;
std::string value() override { return std::to_string(_value); }
};
template <typename T> void add_item(std::map <std::string, std::unique_ptr<generic>> &m, const std::string &n, const T &v) {
m[n] = std::make_unique<special<T>>(typeid(v).name(), v);
}
int
main() {
std::map <std::string, std::unique_ptr<generic>> instances;
add_item<int>(instances, "int", 1);
add_item<bool>(instances, "bool", true);
add_item<float>(instances, "float", 3.1415);
for (auto i : instances) {
std::cout << i.first << " -- " << i.second.get()->name() << " -- " << i.second.get()->value() << std::endl;
}
return 0;
}
不幸的是,我似乎缺少某些内容,因为编译炸弹具有“错误:使用已删除函数”。
有人可以帮我解决这个问题吗?
I originally had a problem creating a map
of classes, with some help
I realized I actually need a map<string, unique_ptr<myclass>>
.
To be more precise:
- I have a bunch of specialized classes with a common ancestor.
- specialized classes are actually specialized from the same template.
- I have a
std::map<std::string, unique_ptr<ancestor>>
taking ownership
of my instances (I hope). - I need ownership because the function creating the instances is in a
completely different section of code than the place using them. - If useful: I create the
map
at initialization time and then I only
reference it at runtime; for all practical purposes after initializing
it (a complex affair, involving reading config files) it could becomeconst
.
A minimal example of what I need to achieve is:
#include <iostream>
#include <string>
#include <memory>
#include <map>
class generic {
std::string _name;
public:
generic(std::string name) : _name(name) {}
virtual ~generic() = default;
virtual std::string name() { return _name; }
virtual std::string value() { return "no value in generic"; }
};
template <class T> class special : public generic {
T _value;
public:
special(std::string name, T value) : generic(name), _value(value) {}
virtual ~special() = default;
std::string value() override { return std::to_string(_value); }
};
template <typename T> void add_item(std::map <std::string, std::unique_ptr<generic>> &m, const std::string &n, const T &v) {
m[n] = std::make_unique<special<T>>(typeid(v).name(), v);
}
int
main() {
std::map <std::string, std::unique_ptr<generic>> instances;
add_item<int>(instances, "int", 1);
add_item<bool>(instances, "bool", true);
add_item<float>(instances, "float", 3.1415);
for (auto i : instances) {
std::cout << i.first << " -- " << i.second.get()->name() << " -- " << i.second.get()->value() << std::endl;
}
return 0;
}
Unfortunately I seem to be missing something because compilation bombs with "error: use of deleted function".
Can someone be so kind to help me sort this out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在此循环中,您尝试复制
unique_ptr
s,但unique_ptr
复制构造函数已被删除。您需要通过引用将它们取:
In this loop you try to copy
unique_ptr
s, but theunique_ptr
copy constructor is deleted.You need to take them by reference instead: