I have one class member of another. I want to convert a shared pointer to master class to that of member. I.e both objects have shared lifetime and the last one to go out of scope will destroy both of them correctly.
#include<memory>
struct Mem
{
};
struct Cont
{
Mem m;
};
I tried the following which works but not perfect. Especially mptr.unique() will not be correct. Is there a better way to correctly share the reference counters?
void foo()
{
std::shared_ptr<Cont> contPtr = std::make_shared<Cont>();
std::shared_ptr<Mem> mptr(&contPtr->m, [contPtr](Mem*){});
}
Aucun commentaire:
Enregistrer un commentaire