Implimenting a class, and I need new to return NULL if the contructor function encounters an error. Any ideas?
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.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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).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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Aww bloody hell. I hate supporting code written by wankers. Thx anyway guys.