[C++-sig] Pointer concept help (original) (raw)

Roman Yakovenko roman.yakovenko at gmail.com
Wed Dec 14 12:28:28 CET 2005


On 12/14/05, dueymk <dueymk at everestkc.net> wrote:

I've been reading docs and googling for 2 days trying to find an answer.

With the following C++ definitions: struct MyStruct1 { int sample1; int sample2; } struct MyStruct2 { int first; char second; MyStruct1 *child; }; class MyClass { MyStruct2 *CreateStruct()

You'd better use std::auto_ptr, this will allow you to not specify call policies.

{ MyStruct2 *returnValue = new MyStruct2; returnValue->child = new MyStruct1; return returnValue; }; }

I'd like to call 'CreateStruct' from Python and access the fields of the actual object that was created in 'CreateStruct', not a copy local to Python. Can I do that and how?

What do you mean "access the fields of the actual object that was created in 'CreateStruct', not a copy local to Python." ?

Also, can I get access to the structure pointed to by the 'child' field?

Yes. You should expose MyStruct1 and MyStruct2

Next code hase been created by pyplusplus for your code:

// std directories: [] // user defined directories: ['c:\temp']

#include "boost/python.hpp"

#include "x.h "

namespace bp = boost::python;

BOOST_PYTHON_MODULE(pyplusplus){ bp::class_< MyClass >( "MyClass" ) .def( "CreateStruct" , (::std::auto_ptr ( ::MyClass::* )( ) )(&MyClass::CreateStruct) , bp::default_call_policies() );

bp::class_< MyStruct2 >( "MyStruct2" )
    .def_readwrite( "first", &MyStruct2::first )
    .def_readwrite( "second", &MyStruct2::second )
    .def_readwrite( "child", &MyStruct2::child );

bp::register_ptr_to_python< std::auto_ptr< MyStruct2 > >();

bp::class_< MyStruct1 >( "MyStruct1" )
    .def_readwrite( "sample1", &MyStruct1::sample1 )
    .def_readwrite( "sample2", &MyStruct1::sample2 );

}

Thanks, Jim

Roman Yakovenko



More information about the Cplusplus-sig mailing list