[C++-sig] access struct inside class

Roman Yakovenko roman.yakovenko at gmail.com
Tue Oct 16 12:21:31 CEST 2007


On 10/14/07, Simon Norberg <simon at dackbrann.net> wrote:
>
> Hello,
> im trying to get python to read and write to a struct inside a class
> in the same way i do in c++
> Customers hej;
> hej.address.contact = "The Devil";
>
> but i cant even get the code to compile, any suggestions?
>
> #include <iostream>
> #include <string>
>
> using namespace std;
> class Customers {
> private:
> struct s_address
> {
> string contact;
> string street1;
> string street2;
> string city;
> string province;
> string postalCode;
> string country;
> };
> public:
> s_address address;
> Customers(){}
> ~Customers(){}
> };
>

1. you cannot export variable, without exporting its type first: you have to
expose s_address class too, if you want to expose address mem. variable.

2. here is the code that was generated by Py++(
http://language-binding.net/), the only change I did for original code
was to change everything to
public.

#include "boost/python.hpp"

#include "1.hpp"

namespace bp = boost::python;

BOOST_PYTHON_MODULE(pyplusplus){
    { //::Customers
        typedef bp::class_< Customers > Customers_exposer_t;
        Customers_exposer_t Customers_exposer = Customers_exposer_t(
"Customers" );
        bp::scope Customers_scope( Customers_exposer );
        bp::class_< Customers::s_address >( "s_address" )
            .def_readwrite( "city", &Customers::s_address::city )
            .def_readwrite( "contact", &Customers::s_address::contact )
            .def_readwrite( "country", &Customers::s_address::country )
            .def_readwrite( "postalCode", &Customers::s_address::postalCode
)
            .def_readwrite( "province", &Customers::s_address::province )

            .def_readwrite( "street1", &Customers::s_address::street1 )
            .def_readwrite( "street2", &Customers::s_address::street2 );
        Customers_exposer.def( bp::init< >() );
        Customers_exposer.def_readonly( "address", &Customers::address );
    }
}


-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20071016/cd456629/attachment.htm>


More information about the Cplusplus-sig mailing list