I'd make safe.open(...) return a Door as a guard. Maybe:
class Door { bool m_open = false; Safe & m_safe; public: Door(Door && door) : m_open(door.m_open), m_safe(door.m_safe) { door.m_open = false; } Door(Safe & safe) : m_safe(safe) {} void opened() { m_open = true; } ~Door() { if (m_open) safe.close(*this); } }; class Safe { // ... Door open( ... ) { Door door(*this); // Do the open; door.opened(); return std::move(door); } // ... };
But I am addicted to RAII, it must be said.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I'd make safe.open(...) return a Door as a guard. Maybe:
But I am addicted to RAII, it must be said.