【问题标题】:Exposing a class with a constructor containing a nested private class in constructor using Boost Python使用Boost Python在构造函数中公开一个包含嵌套私有类的构造函数的类
【发布时间】:2016-10-10 22:21:29
【问题描述】:

我是 Boost Python 的新手,我希望公开一个如下所示的类:

///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();
}

有人可以建议怎么做,因为我尝试过的当前方法会出错,因为 Impl 是私有的,而且访问已经删除的函数错误:

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

【问题讨论】:

    标签: python c++ class boost boost-python


    【解决方案1】:

    pimpl 习惯用法的全部意义在于它是私有的并且对类的用户完全透明。你不要暴露它。

    您需要做的是明确A 不可复制:

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

    【讨论】:

    • 谢谢。这非常有效,就像我这样的新用户顺便说一句,noncopyable 关键字位于 boost 命名空间中。
    • @arunabhsharma noncopyable 是一个,而不是关键字。
    猜你喜欢
    • 2014-06-11
    • 1970-01-01
    • 2012-09-14
    • 2021-03-16
    • 1970-01-01
    • 2012-07-13
    • 2017-08-17
    • 2017-06-19
    • 1970-01-01
    相关资源
    最近更新 更多