[C++-sig] How to hangle a NULL pointer?

Stefan Seefeld stefan at seefeld.name
Tue Oct 2 18:58:49 CEST 2012


I would write little wrapper functions that check the pointer, and if
it's NULL, return None instead of the dereferenced value.

To illustrate:

  object get_age(FaceObject &f) { return f.getAge() ?
object(*f.getAge()) : object();}

  ...

  class_<FaceObject, boost::noncopyable>(...).add_property("age", get_age);

Thus, if the pointer is initialized, the value pointed to will be
returned, and if not, None.

-- 

      ...ich hab' noch einen Koffer in Berlin...



More information about the Cplusplus-sig mailing list