...
This version of the code has the advantage that it is exception-safe: if the call of new throws an exception, the original Thingy (and the Widget that it contains) remains unchanged.
Compliant Solution 3
Alternatively, a resource managing smart pointer (or other resource managing container) may be used to hold the external resource:
| Code Block | ||
|---|---|---|
| ||
class Thingy {
public:
...
// compiler-generated copy assignment now correct
private:
std::tr1::shared_ptr<Widget> w;
};
|
This version of the code is also exception-safe, because the implementation of shared_ptr is exception-safe.
Risk Assessment
Allowing a copy assignment operator to corrupt an object could lead to undefined behavior.
...