gpt4 book ai didi

c++ - CRTP如何使派生类具有基类的容器

转载 作者:行者123 更新时间:2023-12-02 10:16:32 25 4
gpt4 key购买 nike

我想使用 CRTP 模拟以下行为:

#include <vector>
#include <memory>

class GameNumber {
public:
~GameNumber(){}
};

class GameNumber_real : public GameNumber {
public:
GameNumber_real (float m) {n = m;}
~GameNumber_real(){}
private:
float n;
};

class GameNumber_sets : public GameNumber {
public:
GameNumber_sets (float n, float m) {
left.push_back (std::make_shared<GameNumber_real>
(GameNumber_real(n)));
right.push_back (std::make_shared<GameNumber_real>
(GameNumber_real(m)));
}
~GameNumber_sets(){}
private:
std::vector <std::shared_ptr<GameNumber>> left;
std::vector <std::shared_ptr<GameNumber>> right;
};

我的尝试是:
template <class T>
class GameNumber {}

class GameNumber_real : public GameNumber<GameNumber_real> {
public:
GameNumber_real (float m) {n = m;}
private:
float n;
};

class GameNumber_sets : public GameNumber<GameNumber_sets> {
public:
GameNumber_sets (float n, float m) {
left.push_back (std::make_shared<GameNumber_real>
(GameNumber_real(n)));
right.push_back (std::make_shared<GameNumber_real>
(GameNumber_real(m)));
}
private:
std::vector <std::shared_ptr<GameNumber>> left;
std::vector <std::shared_ptr<GameNumber>> right;
};

编译器错误是:
include/game_number.hpp:44:33: error: no matching function for call to
‘std::vector<std::shared_ptr<GameNumber<GameNumber_sets>>>
::push_back(std::shared_ptr<GameNumber_real>)’
(GameNumber_real(n)));

从消息中我了解到,在 GameNumber_sets 中,GameNumber 解析为 GameNumber_GameNumber_sets_。如果您能告诉我如何实现我所期望的行为,我将非常高兴。

最佳答案

您可以显式指定模板参数,如 GameNumber<GameNumber_real>它会解决你的问题。

您也可以将构造函数参数直接传递给 std::make_shared .

改进的代码:

template <class T>
class GameNumber {};

class GameNumber_real : public GameNumber<GameNumber_real> {
public:
GameNumber_real (float m) {n = m;}
private:
float n;
};

class GameNumber_sets : public GameNumber<GameNumber_sets> {
public:
GameNumber_sets (float n, float m) {
left.push_back (std::make_shared<GameNumber_real>(n));
right.push_back (std::make_shared<GameNumber_real>(m));
}
private:
std::vector <std::shared_ptr<GameNumber<GameNumber_real>>> left;
std::vector <std::shared_ptr<GameNumber<GameNumber_real>>> right;
};

但是我不明白为什么在这里需要 CRTP,在您的示例中没有使用它。

编辑:

据我所知,您不能使用 CRTP 作为接口(interface)。但是有 std::any ( https://en.cppreference.com/w/cpp/utility/any ) 如果 c++17 适合您,这可能会有所帮助。

代码如下所示:
template <class T>
class GameNumber {};

class GameNumber_real : public GameNumber<GameNumber_real> {
public:
GameNumber_real (float m) {n = m;}
private:
float n;
};

class GameNumber_sets : public GameNumber<GameNumber_sets> {
public:
GameNumber_sets (float n, float m) {
left.push_back (std::make_shared<std::any>
(GameNumber_real(n)));
right.push_back (std::make_shared<std::any>
(GameNumber_real(m)));
}
private:
std::vector <std::shared_ptr<std::any>> left;
std::vector <std::shared_ptr<std::any>> right;
};

关于c++ - CRTP如何使派生类具有基类的容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61763077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com