![]() |
Inheritance |
![]() |
![]() |
![]() |
これまでの例題はクラスを多様な形態ではないものとしてあつかってきた。 こういうケースは少ない。 多くの場合、多様なクラスや継承により階層化されたクラスをラッピングするだろう。 しばしば、抽象基底クラスから派生するクラスのために Boost.Python ラッパーを書く必要がある。
このつまらない継承クラスで考えよう。
struct Base { virtual ~Base(); };
struct Derived : Base {};
そしてBaseとDerivedオブジェクトの実体を操作する C++ 関数セット。
void b(Base*);
void d(Derived*);
Base* factory() { return new Derived; }
基底クラスBaseのラッピングはこのように表現する。
class_<Base>("Base")
/*...*/
;
これでやっとDerivedとその基底クラスBaseの継承関係を Boost.Python に情報としてあたえることができる。
class_<Derived, bases<Base> >("Derived")
/*...*/
;
こうすることで幾つかのことが自由になる。
C++ の自由な関数 bとdとfactory を開示する。
def("b", b);
def("d", d);
def("factory", factory);
自由な関数factoryは Derivedクラスの新しいインスタンスを生成していることに注意しなさい。 このような場合、return_value_policy<manage_new_object>を使う。 これはBaseへのポインタを採用するように、 また新しいオブジェクト Python Baseが破壊されるまで、 その中に実体を保持するよう Python に指示する。
// Tell Python to take ownership of factory's result
def("factory", factory,
return_value_policy<manage_new_object>());
![]() |
![]() |
![]() |
Copyright © 2002 David Abrahams
Copyright © 2002 Joel de Guzman
Permission to copy, use, modify, sell and distribute this document
is granted provided this copyright notice appears in all copies. This document
is provided "as is" without express or implied warranty, and with
no claim as to its suitability for any purpose.