3 ms·
I'd have liked better ptr features. The auto-cleanup is good. Is it flexible enough to do other things like: auto-close (a file ptr); auto-deref (a ref-counted
by JoeAltmaier 15y ago
I'd have liked better ptr features. The auto-cleanup is good. Is it flexible enough to do other things like: auto-close (a file ptr); auto-deref (a ref-counted thing); auto-unlock (a critical section) etc?
- mkl 15y agoAll of these can be done in a class's destructor, which is called when the object is deleted, e.g. when a shared_ptr's ref-count reaches 0 (i.e. there are no references to it left). To express observation (and break cycles) there is weak_ptr, and to express ownership there is unique_ptr. IMO the new pointer stuff is excellent, and relieves the large majority of the memory management burden.
- reddit_clone 15y agoThere are some boost macros which will run clean up code at the end of the scope if you prefer. They are not pretty. But they work. (This is a life saver in a long function with multiple exit points)
- njs12345 15y agoI haven't read TFA, but all of this stuff is commonly done with RAII in C++: http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization#Typical_uses http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initial... I think the main change C++11 makes in this area is the introduction of std::unique_ptr.