Search code examples
pythonc++classboostboost-python

Exposing a class with a constructor containing a nested private class in constructor using Boost Python


I'm new to Boost Python and I'm looking to expose a class that looks like this:

///Header File structure
class A
{ public:
    A();
    ~A();
    void B();
  private:
    class Impl;
    std::unique_ptr Impl impl_;
};
///Class Implementation
class A::Impl
{
  public:
  void C();    
}
A::A():impl_(new Impl)
{
}
A::~A()
{
}
void A::B()
{
void C();
}

Can someone suggest how to do it since the current methods I've tried gives errors since Impl is private and also an accessing already deleted function error:

BOOST_PYTHON_MODULE(libA)
 {
class_<A::Impl>("Impl")
  .def("C", &A::Impl::C)
class_<A>("A",init<std::unique_ptr>)
  .def("B", &A::B)
  }

Solution

  • The whole point of the pimpl idiom is that it's private and completely transparent to the users of the class. You don't expose it.

    What you do need to do is make it clear that A isn't copyable:

    class_<A, noncopyable>("A", init<>())
        .def("B", &A::B)
    ;