loading...

re: Non isomorphic C++ refactoring VIEW POST

TOP OF THREAD FULL DISCUSSION
re: That's a good point. For instance with: class Safe { void dust() { std::cout << "Removing dust from the safe" << std::endl; }...
 

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.

Code of Conduct Report abuse