dimanche 7 juin 2020

Is there any portential problem of the usage of `std::unique_ptr` between base and derived class?

The related code is given below; you can check it on https://godbolt.org/z/h3GJE-. Is there any portential problem that i should be aware of? Does it need to be improved?

#include <iostream>
#include <memory>

using namespace std;

class Derived;

class Base { 
public:
    static unique_ptr<Base>& get_instance();

private:
    static unique_ptr<Base> pBase;
};

class Derived: public Base { };

std::unique_ptr<Base> Base::pBase = nullptr;

unique_ptr<Base>& Base::get_instance()
{
     pBase = make_unique<Derived>();
     return pBase;
}

int main () 
{
    auto& instance =  Base::get_instance();
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire