Testclass():sources(new int[32]){}
Using member-initialization-list , which is the preferred method of initializing members.
By "safe" or "good," you probably meant, is it safe? What if newthrows a bad_alloc exception?
Well, in this case, the destructor will not be called , because the object is not completely constructed, since the constructor body is not executed. A resource leak may occur if you purchase a resource in the initialization list.
,
class X
{
int *ints;
Huge *huges;
X() : ints(new int[32]), huges(new Huge[10000]) {}
};
new Huge[10000] , , ints, !
function-try-block. :
, , , , . , , , , , , catch -try-block. .
, , .