栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

BOOST类型转换–虚函数和纯虚函数的转换

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

BOOST类型转换–虚函数和纯虚函数的转换

BOOST类型转换–虚函数和纯虚函数的转换 虚函数的转换

1.提供C++代码如下

class VFObj
{
public:
	virtual ~VFObj() {};
	virtual int func() {
		return 0;
	};
};

class VFObjWrapper :public VFObj, public boost::python::wrapper
{
public:
	int func()
	{
		if (boost::python::override func = this->get_override("func"))
		{
			return func();
		}
		return VFObj::func();
	}
	int default_func()
	{
		return this->VFObj::func();
	}
};

2.提供转换代码如下

BOOST_PYTHON_MODULE(Boost_Python_Sample)
{
	boost::python::class_("VFObj")
		.def("func", &VFObj::func, &VFObjWrapper::func);
}

3.提供python测试代码如下

obj = Boost_Python_Sample.VFObj()
print(obj.func())
class newObj(Boost_Python_Sample.VFObj):
    def func(self):
        return 88
obj = newObj()
print(obj.func())
纯虚函数的转换

1.提供C++代码如下

class PVFObj
{
public:
	virtual ~PVFObj() {};
	virtual int func() = 0;
};

class PVFObjWrapper :public PVFObj, public boost::python::wrapper
{
public:
	int func()
	{
		return get_override("func")();
	}
};

2.提供转换代码如下

BOOST_PYTHON_MODULE(Boost_Python_Sample)
{
	boost::python::class_("PVFObj")
		.def("func", boost::python::pure_virtual(&PVFObj::func));
}

3.提供python测试代码如下

class new2Obj(Boost_Python_Sample.PVFObj):
    def func(self):
        return 66
obj = new2Obj()
print(obj.func())
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/630363.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号