jeudi 21 octobre 2021

Initialise unique_ptr inside class

I want to initialise the unique pointer inside class after declaration and I tried few ways but unable to resolve the errors..

template <typename T>
struct Destroy
{
    void operator()(T *t) const
    {
        t->destroy();
    }
};

class Test
{
    
    std::unique_ptr<IRuntime, Destroy<IRuntime>> runtime;

public:
    Test()
    {
        /*
        the function createIRuntime() return type is *IRuntime.
        I tried using following but all the ways I got error:
        1. runtime = std::make_unique<IRuntime, Destroy<IRuntime>> (createIRuntime());  
        2. runtime = createIRuntime();  
        3. runtime = std::unique_ptr<IRuntime, Destroy<IRuntime>> (createIRuntime());        
               
                 Works fine if I do follow.
                 std::unique_ptr<IRuntime, Destroy<IRuntime>> runtime(createIRuntime());
        */
        
        /* how to initialize the unique pointer here*/
    }
};

Aucun commentaire:

Enregistrer un commentaire