如何将std :: make_unique <derived>()转换为std :: unique_ptr <base />

时间:2019-04-07 21:40:59

标签: c++ c++14 factory

我正在尝试基于(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吗?

1 个答案:

答案 0 :(得分:0)

鉴于示例代码,我看不到该问题。

此文件已编译并以C ++ 14模式(Clang 10)运行。我填补了示例代码所缺少的一些空白。我看不到您的<?php mb_internal_encoding("UTF-8"); // old security story, $contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : ''; $ReturnInfo = array(); $ReturnInfo['status'] = 'bad world :/'; if ($contentType === "application/json") { //Receive the RAW post data. $content = trim(file_get_contents("php://input")); $decoded = json_decode($content, true); $User = $decoded['Username']; $Pwd = $decoded['Password']; // write data in a File... $fp = fopen('data.txt', 'wb'); fwrite($fp, "last connected user info =\n"); fwrite($fp, 'Username='.$User."\n"); fwrite($fp, 'Password='.$Pwd."\n"); fclose($fp); $ReturnInfo['status'] = 'good world :)'; $ReturnInfo['User_Psw'] = $User.'<=>'.$Pwd; //If json_decode failed, the JSON is invalid. //if(! is_array($decoded)) { ...... } header("Cache-Control: no-cache, must-revalidate"); // force reset cache header("Expires: Mon, 26 Jul 2000 05:00:00 GMT"); header('Content-type: application/json'); echo json_encode($ReturnInfo); exit(0); ?> 函数;那是您的问题所在吗?

LogicOperationFactory::Create()