使用“ unique_ptr”创建``映射''

发布于 2025-02-06 17:19:25 字数 1861 浏览 3 评论 0原文

我最初有一个问题,创建了类的映射,并在一些帮助下 我意识到我实际上需要一个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 become
    const.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九八野马 2025-02-13 17:19:25

在此循环中,您尝试复制unique_ptr s,但unique_ptr复制构造函数已被删除。

for (auto i : instances) {

您需要通过引用将它们取:

for (auto& i : instances) {

In this loop you try to copy unique_ptrs, but the unique_ptr copy constructor is deleted.

for (auto i : instances) {

You need to take them by reference instead:

for (auto& i : instances) {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文