在Python中公开C++独生子

2024-09-28 03:22:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用Boo::Python:

包装C++独生子
class EigenSolver {
    private:
        static EigenSolver* _self;
        static int _refCount;
    protected:
        EigenSolver();
        ~EigenSolver();

    private:
        EigenSolverOptions _options;
        BasicTypes::Array<double> eigenValues;
        BasicTypes::RegularArray <double> eigenVectors;

    public:
        // singleton initialization, returns unique instance
        static EigenSolver* Instance() {
            if (!_self) _self = new EigenSolver();
            return _self;
        }
        // singleton memory free
        void FreeInst() {
            _refCount--;
            if (!_refCount) {
                delete this;
                _self = NULL;
            }
        }
};

包装代码:

^{pr2}$

当我试图编译库时,我得到一个未解决的外部符号错误:

error LNK2001: unresolved external symbol 
"private: static class UTILS::EigenSolver * UTILS::EigenSolver::_self" 
(?_self@EigenSolver@UTILS@@0PEAV12@EA)
PythonBindingsSolverLib.lib
What is the right way to wrap a C++ singleton class?

使用Boosi::Python包装C++独生子类的正确方法是什么?在

提前谢谢你, 伊万。在


Tags: selfifstaticutilsprivateclassintdouble
1条回答
网友
1楼 · 发布于 2024-09-28 03:22:26

使用C++ Singleton design pattern问题中提供的实现解决了该问题:

py::class_<EigenSolver, boost::shared_ptr<EigenSolver>, boost::noncopyable>
    ("EigenSolver", py::no_init)
    .add_property("instance", py::make_function(&EigenSolver::Instance, 
    py::return_value_policy<py::reference_existing_object>()))
;

相关问题 更多 >

    热门问题