jeudi 1 juin 2017

using make_unique and make_shared statically doesn't work

I'm trying to learn the kinds of factory patterns I can use in C++. I'm not sure why I can't return a unique ptr. I can return a shared ptr just fine. Here's my code:

class FactoryMethodExample {
    FactoryMethodExample() {}
public:
    static FactoryMethodExample get_instance() {
            return {};
    }

    static FactoryMethodExample * get_pointer() {
        return new FactoryMethodExample;
    }

    static unique_ptr<FactoryMethodExample> get_unique_instance() {
        return make_unique<FactoryMethodExample>();
    }

    static shared_ptr<FactoryMethodExample> get_shared_instance() {
        return shared_ptr<FactoryMethodExample>();
    }

    void verify() {
        cout << "I exist" << endl;
    }
};

This code doesn't compile. I get this error :

error: calling a private constructor of class 'FactoryMethodExample'
return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));

Aucun commentaire:

Enregistrer un commentaire