samedi 25 février 2017

Whats the rule about a class handling its own instance?

I have this snippet of code in my code base:

inline void Widget::setParent(Widget& roWidget)
{
   auto upoSelf = m_poParent->remove(*this);
   upoSelf->m_poParent = &roWidget;
   roWidget.m_aupoChildren.emplace_back(std::move(upoSelf));
}

In this situation, upoSelf is going to always be a std::unique_ptr to the object that this points to. So in essence, Widget::setParent has behaviour that handles the Widget object inside of its own function.

I'm wondering if there is any kind of UB or rules about how this works.

Aucun commentaire:

Enregistrer un commentaire