[C++-sig] a pointer question

Jaleco Jaleco at gameone.com.tw
Fri Jun 27 04:31:35 CEST 2003


Dear all

        I am trying use boost.python to export freetype library to python.
But I encounter pointer problem.boost  don't know pointer.
How do I export class pointer to python ?


/ Includes
====================================================================
#include <boost/python.hpp>

class  B
{
  public :
   int d ;

};
class A
{
  public :
    A(int d) { pb = new(B);pb->d = d ;} ;
    int getd(void) { return pb->d;} ;
    B *pb ;
};

// Using
=======================================================================
using namespace boost::python;

// Module
======================================================================
BOOST_PYTHON_MODULE(ft2)
{
  class_<A> ("A",init<int>())
    .add_property("d",&A::getd)
    .def_readonly("pb",&A::pb);
  class_<B> ("B")
    .def_readwrite("d",&B::d)  ;

}


C:\source\boost_1_30_0\libs\python\ft2>python
Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ft2
>>> a = ft2.A(1)
>>> pb = a.pb
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: No to_python (by-value) converter found for C++ type: class B *
>>>


another question is , I try to add
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(B) ;  to
my source source code, This problem is gone.
But the answer is wrong .................

// Includes
====================================================================
#include <boost/python.hpp>


class  B
{
  public :
   int d ;

};
class A
{
  public :
    A(int d) { pb = new(B);pb->d = d ;} ;
    int getd(void) { return pb->d;} ;
    B *pb ;
};
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(B) ;
// Using
=======================================================================
using namespace boost::python;

// Module
======================================================================
BOOST_PYTHON_MODULE(ft2)
{
  class_<A> ("A",init<int>())
    .add_property("d",&A::getd)
    .def_readonly("pb",&A::pb);
  class_<B> ("B")
    .def_readwrite("d",&B::d)  ;

}


Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ft2
>>> a = ft2.A(1)
>>> pb=a.pb
>>> pb
<ft2.B object at 0x009082C8>
>>> pb.d
10445536
>>> a.d
1
>>>






More information about the Cplusplus-sig mailing list