开发者

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

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?


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.


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;
       }
    // ....
    };


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;.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜