std :: make_unique <派生>()をstd :: unique_ptr <base>に変換するにはどうすればよいですか?

Torics

論理演算を登録する(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.

ラムダ関数を作成する代わりに、stackoverflowの他の例に示すようにstd :: move()を使用しようとしました。しかし、それも同じではありません。

メイン

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を作成する方法を誰かに説明してもらえますか?

mciantyre

サンプルコードを考えると、問題はわかりません。

これはコンパイルされ、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;
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

std :: unique_ptr <Derived>をstd :: unique_ptr <Base>に変換しています

分類Dev

std :: make_uniqueとstd :: unique_ptrのnewとの違い

分類Dev

std :: make_uniqueとstd :: unique_ptrの違い

分類Dev

なぜstd :: unique_ptr :: makeではなくstd :: make_uniqueなのですか?

分類Dev

std :: unique_ptr <Dervied>からstd :: unique_ptr <Base>への変換のコストはいくらですか?

分類Dev

make_uniqueを割り当てるには、空のunique_ptrにstd :: move()が必要ですか?

分類Dev

std :: unique_ptr <std :: unique_ptr <T> []>を初期化する方法は?

分類Dev

派生型でstd :: unique_ptr <>を返すことができません

分類Dev

std :: auto_ptrからstd :: unique_ptr

分類Dev

unique_ptr <Derived>をunique_ptr <Base>に変換します

分類Dev

std :: unique_ptr <T []> APIは、派生からベースへのポインター変換を禁止します

分類Dev

std :: unique_ptrを関数に渡すにはどうすればよいですか

分類Dev

std :: moveなしでunique_ptrを値で返すにはどうすればよいですか?

分類Dev

派生クラスでカスタム削除機能を使用してstd :: make_uniqueを使用しますか?

分類Dev

std :: vector <std :: unique_ptr <T >>を別のstd :: vector <std :: unique_ptr <T >>に割り当てます

分類Dev

std :: shared_ptrからstd :: unique_ptrに変換する方法は?

分類Dev

std :: unique_ptrをstd :: setに挿入する

分類Dev

std :: unique_ptrとstd :: map

分類Dev

std :: make_uniqueと配置が新しい

分類Dev

std :: unique_ptr <T>の代わりにstd :: unique_ptr <T>&を使用する利点はありますか?

分類Dev

QVector <std :: unique_ptr <Type >>でstd :: findを使用します

分類Dev

std :: vector <std :: queue <std :: unique_ptr <int >>>のサイズを変更する方法は?

分類Dev

std :: unique_ptrおよびQObject :: deleteLater

分類Dev

is_assignableおよびstd :: unique_ptr

分類Dev

std :: unique_ptrでのclangエラー

分類Dev

boost :: scoped_ptrをstd :: unique_ptrに変更

分類Dev

unique_ptr <Base>を返すように宣言されている関数でunique_ptr <Derived>を返します。

分類Dev

std :: unique_ptrおよびstd :: shared_ptrの所有権を取得する方法

分類Dev

std :: unique_ptrのカスタム削除を安全にオーバーロードするにはどうすればよいですか?

Related 関連記事

  1. 1

    std :: unique_ptr <Derived>をstd :: unique_ptr <Base>に変換しています

  2. 2

    std :: make_uniqueとstd :: unique_ptrのnewとの違い

  3. 3

    std :: make_uniqueとstd :: unique_ptrの違い

  4. 4

    なぜstd :: unique_ptr :: makeではなくstd :: make_uniqueなのですか?

  5. 5

    std :: unique_ptr <Dervied>からstd :: unique_ptr <Base>への変換のコストはいくらですか?

  6. 6

    make_uniqueを割り当てるには、空のunique_ptrにstd :: move()が必要ですか?

  7. 7

    std :: unique_ptr <std :: unique_ptr <T> []>を初期化する方法は?

  8. 8

    派生型でstd :: unique_ptr <>を返すことができません

  9. 9

    std :: auto_ptrからstd :: unique_ptr

  10. 10

    unique_ptr <Derived>をunique_ptr <Base>に変換します

  11. 11

    std :: unique_ptr <T []> APIは、派生からベースへのポインター変換を禁止します

  12. 12

    std :: unique_ptrを関数に渡すにはどうすればよいですか

  13. 13

    std :: moveなしでunique_ptrを値で返すにはどうすればよいですか?

  14. 14

    派生クラスでカスタム削除機能を使用してstd :: make_uniqueを使用しますか?

  15. 15

    std :: vector <std :: unique_ptr <T >>を別のstd :: vector <std :: unique_ptr <T >>に割り当てます

  16. 16

    std :: shared_ptrからstd :: unique_ptrに変換する方法は?

  17. 17

    std :: unique_ptrをstd :: setに挿入する

  18. 18

    std :: unique_ptrとstd :: map

  19. 19

    std :: make_uniqueと配置が新しい

  20. 20

    std :: unique_ptr <T>の代わりにstd :: unique_ptr <T>&を使用する利点はありますか?

  21. 21

    QVector <std :: unique_ptr <Type >>でstd :: findを使用します

  22. 22

    std :: vector <std :: queue <std :: unique_ptr <int >>>のサイズを変更する方法は?

  23. 23

    std :: unique_ptrおよびQObject :: deleteLater

  24. 24

    is_assignableおよびstd :: unique_ptr

  25. 25

    std :: unique_ptrでのclangエラー

  26. 26

    boost :: scoped_ptrをstd :: unique_ptrに変更

  27. 27

    unique_ptr <Base>を返すように宣言されている関数でunique_ptr <Derived>を返します。

  28. 28

    std :: unique_ptrおよびstd :: shared_ptrの所有権を取得する方法

  29. 29

    std :: unique_ptrのカスタム削除を安全にオーバーロードするにはどうすればよいですか?

ホットタグ

アーカイブ