C++初始化std :: vector< std :: simolor_ptr< base>>在派生的类中

发布于 2025-02-03 05:49:55 字数 1258 浏览 3 评论 0原文

我有一个存储std :: vector< std :: simel_ptr< base>>的多态基类。我想在派生的类中初始化此向量。这些向量的尺寸在编译时已知,并且在施工后不会改变。在我当前的解决方案中,我有一个纯虚拟函数initialize(),它在每个派生中都被覆盖:

class Base {
public:
    virtual void Initialize() = 0;
    virtual ~Base() = default;
protected:
    Base(std::size_t count) : m_data(count) {}
    std::vector<std::unique_ptr<Base>> m_data;
};

class Derived1 : public Base {
public:
    Derived1() : Base{ 8 } {}
    void initialize() override {
        m_data[0] = std::make_unique<Derived1>();
        // ...
        m_data[7] = std::make_unique<Derived1>();
    };
};

class Derived2 : public Base {
public:
    Derived2() : Base{ 24 } {}
    void initialize() override {
        m_data[0] = std::make_unique<Derived2>();
        // ...
        m_data[23] = std::make_unique<Derived2>();
    };
};

但是,我对此解决方案不满意;部分是因为使用虚拟功能和
的冗余 m_data [0] = ...; ... m_data [n-1] = ...
我想分配m_data这样:

m_data = {
    std::make_unique<Derived1>(),
    // ...
    std::make_unique<Derived1>()
}

由于std :: simelod_ptr s删除copy copy ctor而无法工作。

这是

” ?我正在使用C ++ 17。

I have a polymorphic base class storing a std::vector<std::unique_ptr<Base>>. I would like to initialize this vector in the derived classes. The sizes of these vectors are known at compile time and do not change after construction. In my current solution I have a pure virtual function initialize() which is overriden in each Derived:

class Base {
public:
    virtual void Initialize() = 0;
    virtual ~Base() = default;
protected:
    Base(std::size_t count) : m_data(count) {}
    std::vector<std::unique_ptr<Base>> m_data;
};

class Derived1 : public Base {
public:
    Derived1() : Base{ 8 } {}
    void initialize() override {
        m_data[0] = std::make_unique<Derived1>();
        // ...
        m_data[7] = std::make_unique<Derived1>();
    };
};

class Derived2 : public Base {
public:
    Derived2() : Base{ 24 } {}
    void initialize() override {
        m_data[0] = std::make_unique<Derived2>();
        // ...
        m_data[23] = std::make_unique<Derived2>();
    };
};

However, I am not pleased with this solution; Partly because of the use of a virtual function and the redundancy of
m_data[0] = ...; ... m_data[N-1] = ....
I would like to assign m_data like this:

m_data = {
    std::make_unique<Derived1>(),
    // ...
    std::make_unique<Derived1>()
}

which does not work because of std::unique_ptrs deleted copy ctor.

Here is a more realistic sample of my code

What is the best way to initialize the vector in the derived classes? I am using C++17.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

小糖芽 2025-02-10 05:49:55

但是,我对此解决方案不满意;部分是由于使用虚拟函数

如果虚拟函数是问题,则将其变成非虚拟函数。

和冗余

而且,您可以简单地使用循环来摆脱重复的

for (auto& ptr : m_data)
    ptr = std::make_unique<Derived1>();

:避免每种类型的代码相同的代码,在C ++,模板中有一个工具:

template<class D, std::size_t N>
struct BaseT : Base {
    BaseT() : Base{N} {}
    void Initialize() override {
        for (auto& ptr : m_data)
            ptr = std::make_unique<D>();
    };
};

struct Derived1 : BaseT<Derived1, 8> {};
struct Derived2 : BaseT<Derived2, 24> {};

在这种情况下,模板参数是派生类型。这种习语有一个名称:奇怪的重复模板模式。

However, I am not pleased with this solution; Partly because of the use of a virtual function

If the virtual function is a problem, then make it non-virtual.

and the redundancy

You can simply use a loop to get rid of the repeated assignments:

for (auto& ptr : m_data)
    ptr = std::make_unique<Derived1>();

To avoid redundancy from having identical code for each type, there is tool for that in C++, templates:

template<class D, std::size_t N>
struct BaseT : Base {
    BaseT() : Base{N} {}
    void Initialize() override {
        for (auto& ptr : m_data)
            ptr = std::make_unique<D>();
    };
};

struct Derived1 : BaseT<Derived1, 8> {};
struct Derived2 : BaseT<Derived2, 24> {};

In this case, the template argument is the derived type. There is a name for such idiom: Curiously Recurring Template Pattern.

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