Consider the following class declaration:
#include "classB.h"
class A {
private:
B *prop;
public:
A() { /* does stuff to set up B as a pointer to a new B */
setB(const B& bar) { /* store a copy of B */
// how do I manage the old *prop?
*prop = new B(bar);
}
};
In setB() how should manage the memory allocation? Should I delete the old *prop? If so, do I dereference and then delete?
In order to maintain the state of the object as it was before the operation in the case that the allocation throws, you might be better off implementing it something like this:
See here (specifically the bit about the strong exception guarantee):
http://en.wikipedia.org/wiki/Exception_guarantees