Managed memory, what do I return?

I'm using std::shared_ptr to manage my memory, but I'm no longer sure what to return, I don't like any of the options I know of (shared_ptr, weak_ptr, pointer, reference)

pointer and reference are too dangerous, since it could be automatically deleted at any time with no way to check if it has been or not when accessed after being stored somewhere.

Entity Manager stores a vector of shared_ptr<Entity>, and returns one when an entity is created.

weak_ptr means I need to use ugly .lock(), so for example:

EntityManager.createEntity()->component<HealthComponent>->health = 10;

would instead be

EntityManager.createEntity()->lock()->component<HealthComponent>->health = 10;

which is just unacceptable, so that is also not an option

but then if something stores the returned shared_ptr, it will introduce another location in which it needs be destroyed for the entity to be deleted, which is also unacceptable

which leaves me with my only real option I know of: return a shared_ptr (looks nice), and store it as a weak_ptr (having to deal with it when stored), but then that's an issue the user (a.k.a me) has to continuously deal with.

is there anything else I can do? I'm not really well versed in all the situations for all the different smart pointers, so any help would be great, I've been struggling with this design decision for a few days.
Last edited on
Topic archived. No new replies allowed.