Implimenting a class, and I need new to return NULL if the contructor function encounters an error. Any ideas?
Printable View
Implimenting a class, and I need new to return NULL if the contructor function encounters an error. Any ideas?
Why not just throw from the constructor?
Im initializing a DirectShow filtergraph, so if it fails I need it to return a NULL pointer, so that the application doesnt try to use the failed class. More issues with supporting existing code that expects a NULL pointer on failure.
No can do. nothrow new will return null on out-of-memory, but if the constructor throws an exception, it throws an exception.
You could initialise the filtergraph pointer to a null pointer constant, and then attempt to assign to it with new (possibly the nothrow version, or simply set the pointer to a null pointer constant in the catch block).Quote:
Im initializing a DirectShow filtergraph, so if it fails I need it to return a NULL pointer, so that the application doesnt try to use the failed class. More issues with supporting existing code that expects a NULL pointer on failure.
EDIT:
Oh yes, the nothrow version of new will not help, so you have to use try and catch anyway.
Aww bloody hell. I hate supporting code written by wankers. Thx anyway guys.