What is the best way to check the pointer return by a new operator
I see following type of code. Assume I have class Test
Type 1
Test *ptr = new Test;
if ( ptr == NULL ) {
}
Type 2
Test *ptr = new Test;
if ( !ptr ) {
}
Type 3
Test *ptr = new Test;
if ( ptr == (Test*)0 ) {
}
You do not check
newfor null it throws an exceptionstd::bad_allocin case it fails.So you handle the exception.
Ofcourse, the above rule applies if you are not using the
nothrowversion ofnew.