Movatterモバイル変換
[0]ホーム
Extending Python with C++ singleton pattern using boost python lib
Benjamin Geerbenjamin.geer at btinternet.com
Wed Apr 25 21:40:46 EDT 2001
In article <9c0tdd02sa7 at news2.newsguy.com>, "Alex Martelli"<aleaxit at yahoo.com> wrote:> It's hard to avoid that: in general, it's VERY hard work to> wrap/expose/handle non-canonical C++ objects (ones that can't be> default-constructed, destructed, copied, assigned). Not just in/to> Python or in/to Boost Python specifically: non canonical objects are Bad> News in any substantial C++ system.It seems to me that this might be true if you pass objects by value and/orreference, but where's the difficulty if you only pass pointers? Asingleton can surely make itself available exactly as described in Gamma et. al,via a static method that returns a pointer to the instance, with verylittle effort required, either in the singleton or in its clients.Copying and assigning pointers is considerably easier than writing copyconstructors and copy assignment methods...If you want polymorphism, you're forced to use pointers or references,but references have to refer to something that lives either on the heapor on the stack. If it lives on the stack, you have all the problems ofcopy constructors, etc. If it lives on the heap, you have to usepointers anyway...If you use garbage collection, there's no way to avoid pointers.Finally, if you're using SWIG to wrap your Python extension, providingpointers to your objects is the the most straighforward approach, unlessyou want to write a lot of glue code by hand.So why not make all your copy constructors private, use pointers foreverything, write singleton classes the way the Gang of Four suggest, andmake a singleton available in a Python module?-- Benjamin Geerhttp://www.btinternet.com/~benjamin.geer
More information about the Python-listmailing list
[8]ページ先頭