Create object using unique_ptr

I have enum class with 3 types of objects. Need to create an appropriate type of object using unique_ptr. But compiler throws lots of errors.

Maybe I am using wrong creation method?
I am new to such things but need to solve it ASAP. Please, help!

Here is my code:

ObjectManager.h

1
2
3
4
5
6
7
8
class ObjectManager
{
public:
    virtual void play(const std::string &sound) const = 0;
    virtual void pause() const = 0;
    virtual void stop() const = 0;
    virtual void resume() const = 0;
};

ObjectType.h

1
2
3
4
5
6
enum class ObjectType
{
    type1,
    type2,
    type3
};


ObjectFactory.h

1
2
3
4
5
6
7
class ObjectFactory
{
private:
    /* data */
public:
    static unique_ptr<ObjectManager> create(ObjectType type);
};

ObjectFactory.cpp

1
2
3
4
static unique_ptr<ObjectManager> create(ObjectType type)
{
    return make_unique<ObjectManager>(type);
}


main.cpp

auto createType = make_unique<ObjectManager>(ObjectType::type1);
Last edited on
auto createType = make_unique<ObjectManager>(ObjectType::type1);
Here you are calling constructor which doesn't exist in class ObjectManager

Did you mean this instead?
auto createType = ObjectFactory::create(ObjectType::type1);

but it will not work anyway because the ObjectFactory::create attempts to copy unique_ptr which is illegal.

You ObjectFactory::create should probably return reference to an instance like this:

1
2
3
4
5
6
7
8
static unique_ptr<ObjectManager>& create(ObjectType type)
{
    // initialized only one, and thus unique
    static auto instance = make_unique<ObjectManager>(type);

    // just return what already exists
    return instance;
}
Last edited on
Tried this.
invalid new-expression of abstract class type 'ObjectManager'
Last edited on
Tried this.
invalid new-expression of abstract class type 'ObjectManager'


The ObjectManager is abstract which means none of it's methods have an actuall implementation, they are pure virtual. = 0

Either implement those methods in ObjectManager, or derive a class from ObjectManager that overrides and implements them.

If you derive your function call should be modified:

1
2
3
4
5
6
7
8
static unique_ptr<ObjectManager>& create(ObjectType type)
{
    // initialized only one, and thus unique
    static auto instance = unique_ptr<ObjectManager>(new DERIVED_TYPE(type));

    // just return what already exists
    return instance;
}


Last edited on
I implemented those methods but now it says
no matching function for call to 'ObjectManager::ObjectManager(ObjectType&)
I'm guessing here. I bet you want to create an object of one of 3 subclasses of abstract base class ObjectManager based on the ObjectType argument passed to create.

If this is the case, then ObjectFactory::create has to check the type argument and create the correct subclass object. Something along these lines:

1
2
3
4
5
6
7
8
9
10
11
12
std::unique_tr<ObjectManager> retPtr;

switch (type)
{
    case type1:
        retPtr.reset(new SubClass1(subclass1Arguments));
        break;
    case type2:
        retPtr.reset(new SubClass2(subclass2Arguments));
        break;
    ...
}


When you use make_unique, you get a unique_ptr of the type of the type being created. You need a unique_ptr to the base class of the type being created. I played with this a bit a little while ago, and the above (reset with new) is the best strategy I came up with. Others here may have better strategies that don't involve new.

Used this
1
2
3
4
5
6
7
8
static unique_ptr<ObjectManager>& create(ObjectType type)
{
    // initialized only one, and thus unique
    static auto instance = make_unique<ObjectManager>(type);

    // just return what already exists
    return instance;
}


And this (in main.cpp)

auto createType = ObjectFactory::create(ObjectType::type1);


Here it says that ObjectFcatory is a deleted function
Okay, doug4, I will try
Topic archived. No new replies allowed.