new operator question

This is a discussion on new operator question within the C++ Programming forums, part of the General Programming Boards category; Implimenting a class, and I need new to return NULL if the contructor function encounters an error. Any ideas?...

  1. #1
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189

    new operator question

    Implimenting a class, and I need new to return NULL if the contructor function encounters an error. Any ideas?
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Why not just throw from the constructor?

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    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.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,112
    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.
    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).

    EDIT:
    Oh yes, the nothrow version of new will not help, so you have to use try and catch anyway.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Aww bloody hell. I hate supporting code written by wankers. Thx anyway guys.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21