What's the best practice to prevent memory leak if an exception thrown in constructor?
问题 I know if an exception is thrown in constructor, destructor will not be called(simple class, no inheritance). So if an exception is thrown in constructor and there is a chance some heap memory is not cleaned up. So what's best practice here? let's assume I have to call some function in constructor and it may throw exception. Shall I always use shared pointer in this case? What's the alternatives? Thank you! 回答1: I would stick to the RAII idiom. If you avoid "naked" resources (like operator