[C++-sig] passing NoneType as argument to constructor

Liam Herron herron at ELLINGTON.com
Fri Jan 29 13:15:58 EST 2016


For the following code:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <boost/python.hpp>
using namespace boost::python;

namespace // unnamed
{

class NullableDouble
{
public:
   NullableDouble()
      : isNull_(true)
      , value_(0.0)
   {
   }

   NullableDouble(double x)
      : isNull_(false)
      , value_(x)
   {
   }

   double value() const
   {
      return value_;  // null check not relevant to this example
   }

   bool isNull() const
   {
      return isNull_;
   }

   // ... more functions but not needed for this example

private:
   bool isNull_;
   double value_;
};

} // end namespace unnamed

void init_pnic()
{
   class_<NullableDouble>
   ("NullableDouble", "NullableDouble", init<>())
      .def(init<double>())
      .def("value", &NullableDouble::value)
      .def("isNull", &NullableDouble::isNull)
   ;
}

BOOST_PYTHON_MODULE(_testPNIC)
{
   init_pnic();
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

I would like to have another constructor method in python that takes a 'None' and returns the same as the no arg constructor 'NullableDouble()'

What is the best way to do this?

Thanks,
--Liam




=============================================================================================
Email transmissions can not be guaranteed to be secure or error-free, as information 
could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain 
viruses.  The sender therefore does not accept liability for any errors or omissions in 
the contents of this message which arise as a result of email transmission.  In addition,
the information contained in this email message is intended only for use of the 
individual or entity named above.  If the reader of this message is not the intended
recipient, or the employee or agent responsible to deliver it to the intended recipient,
you are hereby notified that any dissemination, distribution,or copying of this communication,
disclosure of the parties to it, or any action taken or omitted to be taken in reliance on it,
is strictly prohibited, and may be unlawful.  If you are not the intended recipient please
delete this email message. 
==============================================================================================
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20160129/7e9a5f6f/attachment.html>


More information about the Cplusplus-sig mailing list