It is certainly necessary as you wrote it. Even with delete , however, the class is fundamentally broken because it controls the resource, but does not follow rule three .
Nevertheless, there is practically no reason for manual memory management - it rarely happens. You should probably either just have object B as a member variable, or use a smart pointer like QScopedPointer :
struct A { QScopedPointer<B> b; A() : b(new B()) { } // No ~A() needed; when the A object is destroyed, QScopedPointer ensures // that the B object pointed to by the member 'b' is destroyed and deleted. };
You want to make sure that you have a good C ++ introductory book so that you can learn how to write the right C ++ programs.
James McNellis
source share