Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I cause a new C++ class instance to fail if certain conditions in the constructor are not met?

Tags:

c++

class

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?

like image 314
Jim Fell Avatar asked Apr 27 '10 19:04

Jim Fell


1 Answers

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.

like image 112
Uri Avatar answered Oct 05 '22 03:10

Uri