首 页 | 新 闻 | 技术中心 | 第二书店 | 《程序员》 | 《开发高手》 | 社 区 | 黄 页 | 人 才
移 动专 题SUNIBM微 软微 创精 华Donews人 邮
我的技术中心 
我的分类 我的文档
全部文章 发表文章
专栏管理 使用说明



 RSS 订阅 
最新文档列表
Windows/.NET
.NET  (rss)    
Visual C++  (rss)    
Delphi  (rss)    
Visual Basic  (rss)    
ASP  (rss)    
JavaScript  (rss)    
Java/Linux
Java  (rss)    
Perl  (rss)    
综合
其他开发语言  (rss)    
文件格式  (rss)    
企业开发
游戏开发  (rss)    
网站制作技术  (rss)    
数据库
数据库开发  (rss)    
软件工程
其他  (rss)    

积极原创作者 
tellmenow (22)
cutemouse (22)
softj (78)
iiprogram (69)
qdzx2008 (50)
goodboy1881 (14)
wangchinaking (58)
fancyhf (1)
harrymeng (41)
yjz0065 (113)
CSDN - 文档中心 - 其他开发语言 阅读:3436   评论: 0    参与评论
标题   C++ Boost 之Python(继承)     选择自 Lythm 的 Blog
关键字   Boost Python
出处   http://www.boost.org/

c++boost.gif (8819 bytes)继承

在Python中继承

用Boost.Python扩展的类在Python中支持单继承和多继承.你可以在派生类中任意地混合内建Python类和扩展类.只要Boost.Python 扩展类是在Python中新的类的基类中, 那么结果就是一个扩展的类:

>>> class MyPythonClass:
...     def f(): return 'MyPythonClass.f()'
...
>>> import my_extension_module
>>> class Derived(my_extension_module.MyExtensionClass, MyPythonClass):
...     '''This is an extension class'''
...     pass
...
>>> x = Derived()
>>> x.f()
'MyPythonClass.f()'
>>> x.g()
'MyExtensionClass.g()'

反射C++继承关系

Boost.Python也允许我们提供C++类之间的继承关系,这样派生类就可以作为参数传递到那些需要基类对象的值,指针,引用的地方. class_builder<>的成员方法declare_base就是用来建立基类和派生类之间关系的方法.

#include <memory> // for std::auto_ptr<>

struct Base {
    virtual ~Base() {}
    virtual const char* name() const { return "Base"; }
};

struct Derived : Base {
    Derived() : x(-1) {}
    virtual const char* name() const { return "Derived"; }
    int x;
};

std::auto_ptr<Base> derived_as_base() {
    return std::auto_ptr<Base>(new Derived);
}

const char* get_name(const Base& b) {
    return b.name();
}

int get_derived_x(const Derived& d) {
    return d.x;
}
    
#include <boost/python/class_builder.hpp> // namespace alias for code brevity namespace python = boost::python; BOOST_PYTHON_MODULE_INIT(my_module) { 牋?try 牋?{ 牋牋牋 python::module_builder my_module("my_module"); 牋牋牋 python::class_builder<Base> base_class(my_module, "Base"); 牋牋牋 base_class.def(python::constructor<void>()); 牋牋牋 python::class_builder<Derived> derived_class(my_module, "Derived"); 牋牋牋 derived_class.def(python::constructor<void>()); // Establish the inheritance relationship between Base and Derived derived_class.declare_base(base_class); my_module.def(derived_as_base, "derived_as_base"); my_module.def(get_name, "get_name"); my_module.def(get_derived_x, "get_derived_x"); 牋?} 牋?catch(...) 牋?{ 牋牋牋 python::handle_exception();牋?// Deal with the exception for Python 牋?} }

然后在Python中:

>>> from my_module import *
>>> base = Base()
>>> derived = Derived()
>>> get_name(base)
'Base'
派生的包装类对象可以被传递到需要基类类型的地方
>>> get_name(derived) 
'Derived'
派生的包装类对象可以被传递到需要派生类类型的地方,但是类型信息丢Я?
>>> get_derived_x(derived_as_base()) 
-1

不含虚函数的继承

如果因为某些原因你的基类没有虚函数,但是你仍然希望提供基类和派生类的继承关系,用 boost::python::without_downcast作为declare_base的第2个参数:

struct Base2 {};
struct Derived2 { int f(); };

... 牋 python::class_builder<Base> base2_class(my_module, "Base2"); 牋 base2_class.def(python::constructor<void>()); 牋 python::class_builder<Derived2> derived2_class(my_module, "Derived2"); 牋 derived2_class.def(python::constructor<void>()); derived_class.declare_base(base_class, python::without_downcast);

这个方法允许Derived2对象被传递到需要Base2对象的地方,但是不会引起从Base2灵巧指针到Derived2类型的指针,引用,或者值的隐式转换.

Next: 特殊方法和操作符的支持 Previous: 函数重载 Up: Top

© David Abrahams 2001 版权所有. 本文档允许复制、使用、修改、出售和分发,前提是这个版权声明必须出现在所有的拷贝上。本文档的提供不承担任何直接或隐含的保证,并且不做其适合任一目的之声明。

更新日期: 2000年11月26日


相关文章
对该文的评论