[C++-sig] method chaining in noncopyable derived class

Renato Araujo renatox at gmail.com
Wed Apr 8 18:33:32 CEST 2009


Hi Brian,

in this case I think the correct solution is use "return_self<>()"
verify documentation in:
http://www.boost.org/doc/libs/1_38_0/libs/python/doc/v2/return_arg.html#return_self-spec

BR

On Wed, Apr 8, 2009 at 1:06 PM, Buras, Brian <brian.buras at verigy.com> wrote:
> I'm trying to wrap an API that uses method chaining (methods that return
> reference to same object) and restricts assignment/copy. Is there a reason I
> can't use return_internal_reference?
>
> # want to be able to do this in Python
> d = myexample.Derived("name")
> d.setSomething(500).execute()
>
> But I get a segfault at return of setSomething()
> If I separate the 2 method calls it works.
>
> Here is the API and boost::python wrapper:
> enum MODES {A,B};
>
> class Base {
> public:
>         Base(const string& p);
>         virtual ~Base();
>         void execute(MODES mode = A);
>
> private:
>         string pn;
>         Base& operator=(const Base& rhs);
>         Base(const Base&);
> };
>
> class Derived : public Base {
> public:
>         Derived(const string& p);
>         virtual ~Derived();
>
>         Derived& setSomething(int a);
>
> private:
>         int aa;
> };
>
> BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(d_o,execute,0,1)
>
> BOOST_PYTHON_MODULE(myexample) {
>
>         class_<Base,boost::noncopyable>("Base",no_init);
>         class_<Derived, bases<Base>,boost::noncopyable
>>("Derived",init<string>())
>                 .def("execute",&Derived::execute,d_o())
>                 .def("setSomething",&Derived::setSomething,
> return_internal_reference<>())
>                 //.def("setSomething",&Derived::setSomething,
> return_value_policy<reference_existing_object>())
>                 //.def("setSomething",&Derived::setSomething,
> return_value_policy<copy_non_const_reference>());
> }
>
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>



-- 
Renato Araujo Oliveira Filho


More information about the Cplusplus-sig mailing list