3 ms·
Constructor / Destructor pairs are how you do this in C++. This is so fundamental to the object system that it's a bit steep to claim that "you've never heard o
by robotresearcher 15y ago
Constructor / Destructor pairs are how you do this in C++. This is so fundamental to the object system that it's a bit steep to claim that "you've never heard of it". You have. The author's example of "get the rake - use rake - put away rake" maps exactly to [construct - call methods - destruct].
Given the jmp-iness of exception handling in C++, this is the recommended (i.e. the only safe) way to clean up after yourself.
- humbledrone 15y agoThere's even a commonly used name for it: Resource Acquisition is Initialization (RAII). It's been around for many years, and is fundamental to modern C++ style. It's beyond steep to claim we've never heard of it, just because some language has a feature that helps make it easy. Python has "with," Ruby has blocks, many languages have higher-order functions. Not an uncommon concept.
- deleted 15y ago[deleted]
- dextorious 15y agoThis is just for resource allocation. The pattern the article talks about is FAR more general -- it's about all reversible processes, even things that have nothing to do with allocation.
- hythloday 15y agoSentry classes, which are a generalization of RAII, are used idiomatically in C++ for non-allocation tasks, such as whitespace skipping, logging, and profiling.
- JoeAltmaier 15y agoAlso mapping state to scope e.g. mutex objects that set/clear on ctor/dtor
- masklinn 15y ago> This is just for resource allocation. It's the same thing, you can represent the pair (operation, inverse) as a "resource". Python calls it "context management" (and context managers), the means are slightly different, the result's the same. I agree that OP talks about something of a wider scope though, the language features described in these sub-threads are specialized solutions to precise issues, they're not generalized reversal of arbitrary operations.
- Shorel 15y agoWe can add code to destructors to do general stuff, not only deallocation. The same applies for constructors. In C++ this works amazingly well for objects in the stack (not in the heap, as you need call the destructor yourself).