map<Key, Value>
(and it's mates) holds Value
, well, by value. If you try to give it an instance of derived class to hold as value, most likely "slicing" will occur.
More about slicing: http://stackoverflow.com/q/274626/1125702 https://stackoverflow.com/q/274626/1125702
std::unique_ptr<Component>
seems exactly what you want. It manages lifetime and memory of owned object. By convention, only std::unique_ptr
may "own" it's objects, i.e. anything other may only temporarily refer to given object (either by raw pointer or by references). It is programmer's task to ensure that std::unique_ptr
will outlive any other references to "owned" object.
Usage is quite simple.
// type aliases for convenience
using ComponentPtr = std::unique_ptr<Component>;
using ComponentContainer = std::unordered_map<std::string, ComponentPtr>;
// instance of container
ComponentContainer cc;
// insert component (quick way)
cc["SomeComponent"] = std::make_unique<SomeComponent>(arg1, arg2, ...);
// 'get' returns raw pointer to owned object
SomeComponent * sc_ptr = cc["SomeComponent"].get();
sc_ptr->hello();
// reference, if '->' annoys you
SomeComponent & sc_ref = *cc["SomeComponent"].get();
sc_ref.hello();
map<Key, Value>
(and it's mates) holds Value
, well, by value. If you try to give it an instance of derived class to hold as value, most likely "slicing" will occur.
More about slicing: http://stackoverflow.com/q/274626/1125702
std::unique_ptr<Component>
seems exactly what you want. It manages lifetime and memory of owned object. By convention, only std::unique_ptr
may "own" it's objects, i.e. anything other may only temporarily refer to given object (either by raw pointer or by references). It is programmer's task to ensure that std::unique_ptr
will outlive any other references to "owned" object.
Usage is quite simple.
// type aliases for convenience
using ComponentPtr = std::unique_ptr<Component>;
using ComponentContainer = std::unordered_map<std::string, ComponentPtr>;
// instance of container
ComponentContainer cc;
// insert component (quick way)
cc["SomeComponent"] = std::make_unique<SomeComponent>(arg1, arg2, ...);
// 'get' returns raw pointer to owned object
SomeComponent * sc_ptr = cc["SomeComponent"].get();
sc_ptr->hello();
// reference, if '->' annoys you
SomeComponent & sc_ref = *cc["SomeComponent"].get();
sc_ref.hello();
map<Key, Value>
(and it's mates) holds Value
, well, by value. If you try to give it an instance of derived class to hold as value, most likely "slicing" will occur.
More about slicing: https://stackoverflow.com/q/274626/1125702
std::unique_ptr<Component>
seems exactly what you want. It manages lifetime and memory of owned object. By convention, only std::unique_ptr
may "own" it's objects, i.e. anything other may only temporarily refer to given object (either by raw pointer or by references). It is programmer's task to ensure that std::unique_ptr
will outlive any other references to "owned" object.
Usage is quite simple.
// type aliases for convenience
using ComponentPtr = std::unique_ptr<Component>;
using ComponentContainer = std::unordered_map<std::string, ComponentPtr>;
// instance of container
ComponentContainer cc;
// insert component (quick way)
cc["SomeComponent"] = std::make_unique<SomeComponent>(arg1, arg2, ...);
// 'get' returns raw pointer to owned object
SomeComponent * sc_ptr = cc["SomeComponent"].get();
sc_ptr->hello();
// reference, if '->' annoys you
SomeComponent & sc_ref = *cc["SomeComponent"].get();
sc_ref.hello();
map<Key, Value>
(and it's mates) holds Value
, well, by value. If you try to give it an instance of derived class to hold as value, most likely "slicing" will occur.
More about slicing: http://stackoverflow.com/q/274626/1125702
std::unique_ptr<Component>
seems exactly what you want. It manages lifetime and memory of owned object. By convention, only std::unique_ptr
may "own" it's objects, i.e. anything other may only temporarily refer to given object (either by raw pointer or by references). It is programmer's task to ensure that std::unique_ptr
will outlive any other references to "owned" object.
Usage is quite simple.
// type aliases for convenience
using ComponentPtr = std::unique_ptr<Component>;
using ComponentContainer = std::unordered_map<std::string, ComponentPtr>;
// instance of container
ComponentContainer cc;
// insert component (quick way)
cc["SomeComponent"] = std::make_unique<SomeComponent>(arg1, arg2, ...);
// 'get' returns raw pointer to owned object
SomeComponent * sc_ptr = cc["SomeComponent"].get();
sc_ptr->hello();
// reference, if '->' annoys you
SomeComponent & sc_ref = *cc["SomeComponent"].get();
sc_ref.hello();