[C++-sig] help on abstract class export
Hi, I'm trying to export the following code to python, but failed. Please
help me!
Here is the code:
--
c++ code:
--
class IWorker {
public:
virtual int getData() = 0;
};
class Worker : public IWorker {
public:
int getData() {
return 100;
}
};
extern "C" {
IWorker* createWorker();
}
--
py++ code:
--
mb.free_fun('createWorker').call_policies =
call_policies.return_value_policy(call_policies.return_pointee_value)
--
In python:
--
>>> w = createWorker()
I got 'TypeError: No to_python (by-value) converter found for C++ type:
class IWorker
I found that if the IWorker class is not abstract, for example:
class IWorker {
public:
virtual int getData() { return 0; }
};
Python doesn't report that error, but the result of the following code is
not expected(I think it should be 100).
>>> w = createWorker()
>>> w.getData()
0
How to solve this problem? Please give some advise.
THANK YOU.
--
View this message in context:
http://boost.2283326.n4.nabble.com/help-on-abstract-class-export-tp3483090p3483090.html
Sent from the Python - c++-sig mailing list archive at Nabble.com.
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] help on abstract class export
On Fri, Apr 29, 2011 at 10:14 AM, zeb wrote:
> Hi, I'm trying to export the following code to python, but failed. Please
> help me!
> Here is the code:
> --
> c++ code:
> --
> class IWorker {
> public:
> virtual int getData() = 0;
> };
>
> class Worker : public IWorker {
> public:
> int getData() {
> return 100;
> }
> };
>
> extern "C" {
> IWorker* createWorker();
> }
>
> --
> py++ code:
> --
> mb.free_fun('createWorker').call_policies =
> call_policies.return_value_policy(call_policies.return_pointee_value)
If createWorker is implemented as "return new Worker();", then you
should use "return_value_policy"
(
http://www.boost.org/doc/libs/1_46_1/libs/python/doc/tutorial/doc/html/python/functions.html#python.call_policies
)
If you change the function return type to "std::auto_ptr<...>" then
you don't need to specify the call policies.
HTH
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] help on abstract class export
Thank you very much. It works! -- View this message in context: http://boost.2283326.n4.nabble.com/help-on-abstract-class-export-tp3483090p3483251.html Sent from the Python - c++-sig mailing list archive at Nabble.com. ___ Cplusplus-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] help on abstract class export
On 04/29/11 09:14, zeb wrote:
mb.free_fun('createWorker').call_policies =
call_policies.return_value_policy(call_policies.return_pointee_value)
you do not want to have *value* of an abstract class.
I found that if the IWorker class is not abstract, for example:
class IWorker {
public:
virtual int getData() { return 0; }
};
Python doesn't report that error, but the result of the following code is
not expected(I think it should be 100).
You expectancy is wrong. If you keep IWorker by value there will be no dynamic
dispatch.
This is not related to py++, python or boost::python. Its just the same as in
c++. If you wrote a c++ function that takes IWorker parameter by value you'd get
the same behavior.
Basic rule of thumb: if you're dealing with inheritance, do not pass object by
value.
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
