Since SomeObjectManager
is the owner of the SomeObject
instances (stored in its std::vector
data member), I'd just return raw pointers, since they are actually observing pointers.
std::vector<SomeObject> _heldObjects;
SomeObject* getSomeObjectByName(const std::string& nameToFind) {
... find index of object corresponding to 'nameToFind'
return &_heldObjects[foundIndex];
}
(Note that I passed nameToFind
using reference to const
, since I assume that nameToFind
is an input string, so if inside the method you are just observing that string, you can avoid deep-copies using const &
).
You must pay attention when you have owning raw pointers (they should be wrapped inside safe RAII boundaries), but observing raw pointers are fine.
Just make sure that the lifetime of SomeObjectManager
exceeds that of its clients, to make sure that the clients are referencing valid objects.
Note also that if you add new items to the vector data member (e.g. using std::vector::push_back()
), the addresses of the previous SomeObject
instances stored in the vector can change. So, if you gave pointers to those outside, they become invalid.
So, make sure that the vector size and vector content are not changed before you give pointers to its elements to client code outside.
An alternative would be to have std::vector<std::unique_ptr<SomeObject>>
as data member. In this case, even if the vector is resized, the addresses you returned using the smart pointers (in particular using std::unique_ptr::get()
) are still valid:
std::vector<std::unique_ptr<SomeObject>> _heldObjects;
SomeObject* getSomeObjectByName(const std::string& nameToFind) {
... find index of object corresponding to 'nameToFind'
return _heldObjects[foundIndex].get();
}
PS
Another option might be returning references to const SomeObject
(assuming that this use of const
makes sense in your design):
std::vector<SomeObject> _heldObjects;
const SomeObject& getSomeObjectByName(const std::string& nameToFind) const {
... find index of object corresponding to 'nameToFind'
return _heldObjects[foundIndex];
}