I know constructors don’t “return” anything but for instance if I call CMyClass *object = new CMyClass() is there any way to make object to be NULL if the constructor fails? In my case I have some images that have to be loaded and if the file reading fails I’d like it to return null. Is there any way to do that?
Thanks in advance.
I know constructors don’t return anything but for instance if I call CMyClass *object
Share
I agree with everyone else that you should use exceptions, but if you do really need to use NULL for some reason, make the constructor private and use a factory method:
This means you can’t construct instances normally though, and you can’t allocate them on the stack anymore, which is a pretty big downside.