classSafe{voiddust(){std::cout<<"Removing dust from the safe"<<std::endl;}}intmain(){Safesafe;safe.open(combination,key);safe.putDocuments(documents);safe.dust();}
You will get:
Opening door
Putting passport in safe
Putting ID card in safe
Removing dust from the safe
Closing door
Which is probably not want you want. That's the issue with contrived examples :)
We can make it work by having:
Get rid of Door destructor
Add an explicit .close() method in the Safe class though
Store the state of the safe inside a member of the class
That's a good point. For instance with:
You will get:
Which is probably not want you want. That's the issue with contrived examples :)
We can make it work by having:
Door
destructor.close()
method in theSafe
class thoughI'll update the article, thanks.
I'd make safe.open(...) return a Door as a guard. Maybe:
But I am addicted to RAII, it must be said.