How to initialize a unique_ptr

I think animal.reset(new Animal(file)) is what you want.

http://en.cppreference.com/w/cpp/memory/unique_ptr/reset


#include<iostream>
#include<memory>
#include<iostream>

class Amm{

    public:
    std::unique_ptr<double> myVar;
    explicit Amm(std::unique_ptr<double> ptr):myVar{ptr.release()}{}
};

int main(){
    Amm a(std::make_unique<double>(5));
    std::cout<<*a.myVar;

    return 0;

}

#include <memory>
#include <algorithm>
#include <iostream>
#include <cstdio>

class A
{
public :
    int a;
    A(int a)
    {
        this->a=a;

    }
};
class B
{
public :
    std::unique_ptr<A> animal;
    void Init(int a)
    {
        this->animal=std::unique_ptr<A>(new A(a));
    }
    void show()
    {
        std::cout<<animal->a;
    }
};

int main()
{
    B *b=new B();
    b->Init(10);
    b->show();
    return 0;
}