tags:

views:

113

answers:

3

As I understand it, when a new class is instantiated in C++, a pointer to the new class is returned, or NULL, if there is insufficient memory. I am writing a class that initializes a linked list in the constructor. If there is an error while initializing the list, I would like the class instantiator to return NULL.

For example:

MyClass * pRags = new MyClass;

If the linked list in the MyClass constructor fails to initialize properly, I would like pRags to equal NULL. I know that I can use flags and additional checks to do this, but I would like to avoid that, if possible. Does anyone know of a way to do this?

Thanks.

+15  A: 

The common approach here is to throw an exception (and handle it somewhere higher up).

One of the benefits of the exception mechanism is that it allows you to throw an exception from within a class constructor. In that case, you never reach the situation where a pointer is returned to the invalid. You would "get control" at the corresponding catch block. If the pointer was only declared within the try block (or in some other method invoked by the try block), it would be outside your scope in that catch block.

That is not a hack - it is quite legitimate and a common programming technique. For example, if your class constructor allocates memory dynamically (e.g., for an internal buffer) and this allocation failed, you would want to throw an exception because at the end of the constructor body you would not have a valid object.

Here is an example (I've been doing Java for the past 10 years, so my C++ code below is likely messed up here, maybe someone can edit this for me)

// Begin C++ code written by a Java programmer... :)
class Myclass
{
   public:
      Myclass(int length)
      {
          if(length<=0) throw BadBufferSizeException("Bla bla bla");         
          this->buffer = (char*)malloc(length*sizeof(char)); // don't remember the new syntax
      }

      void doSomething()
      {
          // Code for placing stuff in the buffer
      }
    private:
      char* buffer;
};


int main()
{
   try
   { 
     int len;
     len = getLengthFromUser();
     MyClass* pMyClass = new MyClass(len);
     myClass->doSomething();
    } catch(const Exception & e)
    {
       // Whatever... Note how pMyClass is not even accessible here
    }
}

Note that if you defined pMyclass to be null outside the try block, and then only reassigned it within the try block when you create the class, in the case of failure you would likely still have null, but you would never have executed doSomething(). If you are concerned about initialization, you could also move the doSomething() call to outside the try-catch block, but you would want to make sure that your pointer is not null.

Also note that C++ gives you more (too much?) freedom when it comes to throwing things than other languages. I usually like having a hierarchy of exception classes or using an existing library with such a hierarchy.

Uri
This is also what the C++ FAQ recommends http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.2
Nick Meyer
Thanks, Uri. So, would I do something like `try { if ( !This() ) throw 20; }` in the constructor and omit the `catch` statement, or is the `catch` statement needed?
Jim Fell
@Jim: Let me edit my answer with an example
Uri
I see, now. Thanks!
Jim Fell
@Uri, close, but if you throw by pointer you must catch by pointer as well. I'd recommend throwing by value and catching by `const` reference, though (edited).
Nick Meyer
@Nick: Good point... That was pure Java syntax, I'm afraid :( It's amazing how much of a language you forget by writing in a language that looks a lot like it.
Uri
+1  A: 

The standard way for a constructor to indicate an error is with an exception.

If, for some reason, your application can't deal with exceptions and needs the NULL return value, you can wrap the constructor in factory method like this...

class MyClass
{
    public:
        static MyClass* makeMyClass()           
        {
           MyClass* retval = NULL;
           try 
           {
              retval = new MyClass();
           }
           catch(std::exception& e)
           {
               // Report the exception;
               retval = NULL;
           }
           return retval;
       }
    // ....
    };
JohnMcG
This is a good alternative if you don't want your clients to have to handle the exception.
Nick Meyer
A: 

Your understanding is somewhat old, as the 1995 Standard prescribed the use of exceptions instead of returning 0. It also provided for a nothrow series of methods, which work as follows: Foo * foo = new (nothrow) Foo;.

David Thornley
I think he's referring to the constructor failing, not the memory allocation for the new object failing.
Nick Meyer
Just to be clear, ever *since* 1995 that has been how it is done. `std::bad_alloc` was not obsoleted 15 years ago.
Potatoswatter