1

I have a virtual class in my code:

namespace X
{
    class Y
    {


    public:
        /* CTOR */
        Y() {};

        /* DTOR */
        virtual ~Y() {};
        
        /* Initialization */
        virtual bool Init() = 0;

        virtual int Foo(void** ptr, size_t size) = 0;
        
        virtual int Foo2(void *ptr) = 0;


    private:

    };
}

I need to define the variable in python using cppyy:

std::shared_ptr<X::Y> tempValue = nullptr;

I have tried

self.allocMgr = cppyy.gbl.MEMORY_ALLOCATOR.Allocator.__smartptr__()

It didn't work.

Melebius
  • 6,183
  • 4
  • 39
  • 52

1 Answers1

0

What is not working for you with cppyy.gbl.std.shared_ptr['X::Y']()?

Nominally, using cppyy.gbl.std.make_shared is the easiest approach (just as it is for C++), but in this case the template can not be directly instantiated because the class in the example is virtual. However, you can still create a typed nullptr using cppyy.bind_object(cppyy.nullptr, cppyy.gbl.X.Y) which can be used as an argument to aid implicit template instantiations.

Note that if std::shared_ptr<X::Y> tempValue = nullptr; is what you're looking for, and the Python code isn't obvious, then it's always possible to use cppdef as a workaround, eg.:

cppyy.cppdef("std::shared_ptr<X::Y> tempValue = nullptr;")
ptr = cppyy.gbl.tempValue
Wim Lavrijsen
  • 3,453
  • 1
  • 9
  • 21