Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object Pointer Status After New Constructor Throws Exception - C++

What is going to be the status of an object pointer when a new class constructor throws an exception in C++? Take the following code for example:

CMyClass * pobjMyClass = (CMyClass *)0xA5A5A5A5;

try
{
    pobjMyClass = new CMyClass(); // Exception thrown in constructor
}
catch ( ... ) {}

When this code executes what will the value of pobjMyClass be, after the exception is thrown? A pointer to an invalid instance of CMyClass, 0xA5A5A5A5, NULL, some random uninitialized value, or something else? Thanks.

like image 396
Jim Fell Avatar asked Nov 30 '10 20:11

Jim Fell


1 Answers

Since the exception is thrown before the assignment takes place, pobjMyClass will be whatever it was before - in your case, 0xa5a5a5a5.

like image 192
EboMike Avatar answered Nov 15 '22 03:11

EboMike