如何将标准::make_unique<derived>() 转换为标准::unique_ptr<base>
How can I convert std::make_unique<derived>() to std::unique_ptr<base>
我正在尝试构建一个基于(https://www.codetg.com/article/7r1QnR43bm3ZogBJ.html(的自注册工厂方法,该方法注册逻辑操作。但是我不知道如何将标准::make_unique转换为标准::make_unique。我总是收到相同的错误:
return': cannot convert from 'std::unique_ptr<T1,std::default_delete<_Ty>>' to 'std::unique_ptr<LogicOperation,std::default_delete<_Ty>>
我仍然是唯一指针主题的菜鸟,但我已经阅读了 cppreference.com
If T is a derived class of some base B, then std::unique_ptr<T> is implicitly convertible to std::unique_ptr<B>.
The default deleter of the resulting std::unique_ptr<B> will use operator delete for B,
leading to undefined behavior unless the destructor of B is virtual.
我尝试使用std::move((而不是制作lambda函数,如堆栈溢出上的其他示例所示。但这也不起作用。
主要
int main()
{
Signal a;
Signal b;
a.setState(1);
b.setState(0);
std::unique_ptr<LogicOperation> logic = LogicOperationFactory::Create("AND");
bool x[2] = { a.getState(), b.getState() };
bool y = logic->operation(x, 2); // do and operation
}
LogicOperation.h
class LogicOperation
{
public:
LogicOperation() = default;
virtual ~LogicOperation() = default;
public:
virtual bool operation(bool*, uint8_t count) = 0;
};
LogicOperationFactory.h:
using TCreateMethod = std::function<std::unique_ptr<LogicOperation>()>;
template<class T1>
static bool Register(const std::string name)
{
std::map<std::string, TCreateMethod>::iterator it;
it = s_methods.find(name);
if (it != s_methods.end())
return false;
s_methods[name] = []() -> std::unique_ptr<LogicOperation> {
// Constructs an object of type T and wraps it in a std::unique_ptr
return std::make_unique<T1>(); // use default constructor
};
return true;
}
逻辑与操作.cpp
class LogicAndOperation :
public virtual LogicOperation
{
public:
LogicAndOperation() = default;
virtual ~LogicAndOperation() = default;
bool operation(bool* signals, uint8_t count) override;
private:
static bool s_registered;
};
bool LogicAndOperation::s_registered =
LogicOperationFactory::Register<LogicAndOperation>("AND");
有人可以向我解释一下,我如何从派生类(逻辑和操作(制作 std::unique_ptr?
给定示例代码,我看不到问题。
这在C++14模式(Clang 10(下编译并运行。我填补了我们的示例代码所缺乏的一些空白。我看不到你的LogicOperationFactory::Create()
功能;这就是你的问题所在吗?
#include <cassert>
#include <cstdint>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
class LogicOperation
{
public:
virtual ~LogicOperation() = default;
virtual bool operation(bool*, uint8_t count) = 0;
};
using TCreateMethod = std::function<std::unique_ptr<LogicOperation>()>;
class LogicOperationFactory
{
static std::map<std::string, TCreateMethod> s_methods;
public:
template<class T>
static bool Register(const std::string& name)
{
std::map<std::string, TCreateMethod>::iterator it;
it = s_methods.find(name);
if (it != s_methods.end())
return false;
s_methods[name] = []() -> std::unique_ptr<LogicOperation> {
// Constructs an object of type T and wraps it in a std::unique_ptr
return std::make_unique<T>(); // use default constructor
};
return true;
}
static std::unique_ptr<LogicOperation> Create(const std::string& name)
{
auto iter = s_methods.find(name);
return (iter != s_methods.end()) ? (iter->second)() : nullptr;
}
};
std::map<std::string, TCreateMethod> LogicOperationFactory::s_methods;
class FooLogic : public LogicOperation
{
public:
bool operation(bool*, uint8_t) override {
std::cout << "FooLogic::operation" << std::endl;
return true;
}
};
class BarLogic : public LogicOperation
{
public:
bool operation(bool*, uint8_t) override {
std::cout << "BarLogic::operation" << std::endl;
return true;
}
};
static bool s_registeredFooLogic = LogicOperationFactory::Register<FooLogic>("FooLogic");
static bool s_registeredBarLogic = LogicOperationFactory::Register<BarLogic>("BarLogic");
int main() {
assert(s_registeredFooLogic && s_registeredBarLogic);
auto bar_logic = LogicOperationFactory::Create("BarLogic");
bool flag = false;
bar_logic->operation(&flag, 1);
auto null_logic = LogicOperationFactory::Create("ThisDoesNotExist");
assert(nullptr == null_logic);
return 0;
}
相关文章:
- 为什么 std::unique 不调用 std::sort?
- EASTL矢量<向量<int>>连续的
- 生成"unique"矩阵
- 我对 std::unique(算法)C++有问题
- std::shared_ptr::unique(),复制和线程安全
- 如何在C++03中用自定义谓词调用std::unique
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- C 字符串比较“祝您好运”&gt;“再见”
- C++ std::unique并没有显示我对它的期望
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C++14 unique_ptr并使用已删除的函数'std::unique-ptr' unique_ptr错误
- C++ 如何生成 10,000 个 UNIQUE 随机整数以存储在 BST 中?
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- C 操作员&gt;&gt;与突变器过载
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 是否需要使用 - &gt;运算符在C 中调用成员函数时
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11