[C++-sig] Re: Making copy of function derived from C++ pure virtual function

Paul F. Kunz Paul_Kunz at slac.stanford.edu
Wed Dec 8 21:14:21 CET 2004


>>>>> On Wed, 08 Dec 2004 14:44:52 -0500, David Abrahams <dave at boost-consulting.com> said:

> Paul F. Kunz wrote:
>> I have a function written in Python derived from C++.  Now I want
>> to clone this function in C++.  What should I do in the copy
>> constructor of the function wrapper?  I have...

> Way too much detail missing for me to say anything useful.

   Sorry, I'll be more verbose.

> Start by defining your terms (or used established terminology):

> What does it mean to have a "function written in Python derived from
> C++?"  What does it mean to clone a function?

   Following the Boost.Python tutorial, I have a abstract C++ base
class, FunctionBase,  with pure virtual functions.   Derived from it
is my wrapper class, FunctionWrap, which contains the implementation
of the pure virtual functins of the base that uses call_method<> ()
(...) to call the Python function.   I then implement a function in
Python that derives from FunctionBase, FunctionWrap...

   export_FunctionBase ()
    {
      class_ < FunctionBase, FunctionWrap, boost::noncopyable  >
	( "FunctionBase" )


>> FunctionWrap:: FunctionWrap ( const FunctionWrap & wrap )
>>  : FunctionBase ( wrap ),
>>    m_self ( wrap.m_self )
>>  { // what should be here to create a new PyObject that is copy of old one?

> What old one?

The object wrap.m_self which is pointer to PyObject in the constructor...

FunctionWrap::
FunctionWrap ( PyObject * self )
  : FunctionBase (),
    m_self ( self )
{
}


The C++ code makes a copy of the function by calling virtual method
clone(), which is implemented as ...

FunctionBase *
FunctionWrap::
clone () const
{
  return new FunctionWrap ( *this );
}

   So in the FunctionWrap copy constructor, I should make a copy of
the Python * object held by the FunctionWrap object being copied,
otherwise I get a shallow copy.


FunctionWrap::
FunctionWrap ( const FunctionWrap & wrap )
  : FunctionBase ( wrap )
{
m_self = ????(wrap.m_self)???
}

   Complete source code for FunctionWrap with its boost.python module
use can be found here...

http://www.slac.stanford.edu/grp/ek/hippodraw/FunctionWrap_8cxx-source.html



More information about the Cplusplus-sig mailing list