3 ms·
A proper RAII object has no pre-acquisition state, hence the acronym. Such objects are always valid and ready for use. That means resource acquisition has to ta
by to3m 11y ago
A proper RAII object has no pre-acquisition state, hence the acronym. Such objects are always valid and ready for use. That means resource acquisition has to take place in the constructor, which means you need to report errors by throwing if you're not to leave the object in some kind of non-valid state.
(I'd have thought the cleanup-in-the-destructor part was more important, and thus more worthy of being emphasised in the acronym - but there you go.)
- readams 11y agoYes you can use exceptions to handle errors in the constructor, but you can just as easily do: guard x(); if (!x) { // an error occurred return x.error_code(); } x->do_stuff(); You don't have exceptions so of course you're now required to check for errors explicitly.