[C++-sig] wrapping reference members

Matthew Scouten (TT) Matthew.Scouten at tradingtechnologies.com
Wed Jul 22 17:18:05 CEST 2009


Try something like this:

float get_a(const Foo& self) {return self.a}
void  set_a(Foo& self, float a_new) {self.a = a_new }

class_<Foo>("Foo", init<>())
    .def_property("a", &get_a, &set_a)
    ;



-----Original Message-----
From:
cplusplus-sig-bounces+matthew.scouten=tradingtechnologies.com at python.org
[mailto:cplusplus-sig-bounces+matthew.scouten=tradingtechnologies.com at py
thon.org] On Behalf Of Sebastian Kraemer
Sent: Wednesday, July 22, 2009 9:57 AM
To: cplusplus-sig at python.org
Subject: Re: [C++-sig] wrapping reference members

> I would think that .def_readwrite would do what you need. If it
doesn't
> look at using .def_property and get/set pair. Caveat: making sure that
> 'a' stays a valid reference is your problem. Python will happily crash
> horribly (if you are lucky) in response to bogus pointers or
references.
Thanks for your fast answer! I tried it with following code:

class_<Foo>("Foo", init<>())
    .def_readwrite("a", &Foo::a)
    ;

But it raises the error:

error: cannot create pointer to reference member 'Foo::a'

And it seems it's really not possible to create a pointer to a reference
in c++ ;) So what else do I have to write instead of "&Foo::a"?

Cheers,
Sebastian

_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig at python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig


More information about the Cplusplus-sig mailing list