![](/img/trans.png)
[英]How to convert unique_ptr<derived>* to unique_ptr<base>*?
[英]How can I convert std::make_unique<derived>() to std::unique_ptr<base>
我正在嘗試構建一個基於( https://www.codetg.com/article/7r1QnR43bm3ZogBJ.html )自我注冊的工廠方法,它注冊邏輯操作。 但我無法弄清楚如何將std :: make_unique轉換為std :: 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函數,如stackoverflow上的其他示例所示。 但這也不起作用。
主要
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;
}
LogicAndOperation.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");
有人可以向我解釋,我如何從派生類(LogicAndOperation)制作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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.