官方文檔的Wrapper建議
C++代碼:
class TestBase
{
public:
virtual void f()
{
cout<<"cpp: call TestBase f"<<endl;
}
};
class TestBaseWrapper : public TestBase, public wrapper<TestBase>
{
public:
void f() override
{
cout << "cpp: call TestBaseWrapper f" << endl;
if (auto f = this->get_override("f"))
{
f();
return;
}
this->TestBase::f();
}
void default_f()
{
cout << "cpp: call TestBaseWrapper default_f" << endl;
this->TestBase::f();
}
};
class_<TestBaseWrapper, boost::noncopyable>("TestBase", init<>())
.def("f", &TestBase::f, &TestBaseWrapper::default_f)
;
python 調(diào)用
Python測(cè)試代碼:
TestBase().f()
cpp: call TestBaseWrapper default_f
cpp: call TestBase f
class TestPySub1(TestBase):
pass
TestPySub1().f()
cpp: call TestBaseWrapper default_f
cpp: call TestBase f
class TestPySub2(TestBase):
def f(self):
print "py: call TestPySub2 f"
TestPySub2().f()
py: call TestPySub2 f
結(jié)論:
從python層調(diào)用 f()
未覆寫方法 -> TestBaseWrapper::default_f()
已覆寫 -> py覆寫的 f()
C++ 調(diào)用
C++代碼:
void test(TestBase* obj)
{
obj->f();
}
def("test", &test);
Python測(cè)試代碼:
test(TestBase())
cpp: call TestBaseWrapper f
cpp: call TestBase f
class TestPySub1(TestBase):
pass
test(TestPySub1())
cpp: call TestBaseWrapper f
cpp: call TestBase f
class TestPySub2(TestBase):
def f(self):
print "py: call TestPySub2 f"
test(TestPySub2())
cpp: call TestBaseWrapper f
py: call TestPySub2 f
結(jié)論:
從C++層調(diào)用 f()
調(diào)用TestBaseWrapper::f()
未覆寫方法 -> TestBase::f()
已覆寫 -> py覆寫的 f()
嘗試簡(jiǎn)化
C++代碼:
class TestBase :public wrapper<TestBase>
{
public:
virtual void f()
{
cout<<"cpp: call TestBase f"<<endl;
}
void f_()
{
cout << "cpp: call TestBase f_" << endl;
if (auto f = this->get_override("f"))
{
f();
return;
}
this->f();
}
};
class_<TestBase, boost::noncopyable>("TestBase", init<>())
.def("f", &TestBase::f)
;
Python 調(diào)用
Python測(cè)試代碼
TestBase().f()
cpp: call TestBase f
class TestPySub1(TestBase):
pass
TestPySub1().f()
cpp: call TestBase f
class TestPySub2(TestBase):
def f(self):
print "py: call TestPySub2 f"
TestPySub2().f()
py: call TestPySub2 f
結(jié)論:
從python層調(diào)用 f()
未覆寫方法 -> TestBase::f()
已覆寫 -> py覆寫的 f()
C++ 調(diào)用
C++代碼:
void test(TestBase* obj)
{
obj->f_();
}
def("test", &test);
Python測(cè)試代碼:
test(TestBase())
cpp: call TestBase f_
cpp: call TestBase f
class TestPySub1(TestBase):
pass
test(TestPySub1())
cpp: call TestBase f_
cpp: call TestBase f
class TestPySub2(TestBase):
def f(self):
print "py: call TestPySub2 f"
test(TestPySub2())
cpp: call TestBase f_
py: call TestPySub2 f
結(jié)論:
從C++層調(diào)用 f()
調(diào)用TestBase::f_()
未覆寫方法 -> TestBase::f()
已覆寫 -> py覆寫的 f()