mardi 30 août 2016

interface function getting rvalue pointers to initialize shared_ptr

I have a class exposing through it's interface add function:

void AddObject(Object *o);

Inside the class I maintain the objects in set<shared_ptr<Object>>. Since I will create shared_ptr from the received pointer I thought to limit the function argument to only rvalue pointers so to make sure that the user will not delete the pointer I use. And so I'll change the function declaration to:

void AddObject(Object* &&o);

so a typical use will be:

AddObject(new Object())

preventing the user to accidentally delete pointer I hold. I don't want to to use shared_ptr in the interface because the user is not familiar with shared_ptr. Do you think my suggestion is a good idea?

Aucun commentaire:

Enregistrer un commentaire