简体   繁体   中英

How to wrap c++ object with boost.Python, so that Python will never call it destructor automatically

For example, I have C++ class "A", and python class "B". Class "A" wrapped with boost::python, so I can use it in my python code. Class "B" has a member of type "A", I create it in constructor of "B". When I remove my object of class "B", it automatically calls destructor of "A" in C++. I want to avoid this call, so that "B" will not be responsible for its member "A" (that is wrapped c++ object). So, I want to remove my "B" object, and still have "A" in memory.

You should keep a pointer in your "B Class" to the "A Class object", So that when the "B object" is destructed the "Class A Object" is not reclaimed.

EDIT:

I think this could solve your problem:

aObjectPTR = POINTER(aObj)

Now, you have a pointer called "aObjectPTR". place instead of "AObj" the name of your A Class Object. After that, you have a pointer pointed to the A Class Object, so that the pointer(aObjectPTR) will only be reclaimed, not the object it point to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM