Pergunta

I have C++ code (not mine, so it is not editable). Problem is with extending protected functions and class.

#include "ExtraClass.h"
...
MyClass::MyClass()
{
...
protected:
    bool Func{}
    ExtraClass m_Foo;
...
}

I need access in Python to m_Foo methods and protected functions like Func() like

from MyClass import  *
bar = MyClass()
bar.m_Foo.Run() //something like this

but have an compiler error: *error: ‘ExtraClass MyApp::m_Foo’ is protected*

PS. If I change protected with public (just for try). I can access *m_Foo* only in readonly mode:

class_<MyClass>("MyClass", init<>()) 
    .def_readonly("m_Foo", &MyClass::m_Foo)

Changing to *def_readwrite* went to compiler error:

/boost_1_52_0/boost/python/data_members.hpp:64:11: error: no match for ‘operator=’ in ‘(((ExtraClass)c) + ((sizetype)((const boost::python::detail::member<ExtraClass, MyClass>*)this)->boost::python::detail::member<ExtraClass, MyClass>::m_which)) = d’

Thank you for any help!

Foi útil?

Solução

In general, if you want to wrap protected members, then you need to derive a (wrapper) class from the parent that makes the members public. (You can simply say using Base::ProtectedMember in a public section to expose it instead of wrapping it). You will then have wrap it normally. Like this:

class MyWrapperClass : public MyClass {
  public:
    using MyClass::m_Foo;
};

In this particular example (which is really not fully baked), if you want to access m_Foo, then you need to wrap ExtraClass. Assuming that you have The problem with readwrite is likely the implementation of ExtraClass (which probably doesn't supply a operator= that you can use).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top