Malloc in C ++ Constructor

I need to interact with some C code from the C ++ class constructor (Intel library)


  class A{
    A{
     x = ippiMalloc();
     if(x==NULL) ...
    }
  }

The constructor uses the malloc function (intel version). If the ippiMalloc function failed, how to handle it correctly. Throw an exception?

+5
source share
3 answers

Yes, an exception is likely to be the most appropriate way to handle the error here - at least it will be the cleanest way. It also means that the “new” operator will fail.

If your application is no exception, then you have a problem. In this case, the factory method in the class may make sense.

static A * create() { ... }

, , null, .

+8

, :) , , , , , bool , object_state_is_not, - .

+4

Joining the previous answers - and here - a good explanation of why exceptions are the best way to handle errors in constructors. (C ++ FAQ Lite)

+1
source

All Articles