C++ const pointer not recognized as C++ at compilation time

101 views
Skip to first unread message

Sandra Hicks

unread,
Dec 5, 2016, 12:16:45 PM12/5/16
to cython-users
Hello,
I am encountering another problem. I have been wrapping C++ classes and I need to deal with pointers to C++ const objects (const Foo* myFoo). Therefore I have implemented classes that hold a pointer and a pointer to a const, which I use as I need.

cdef class Foo:
    cdef cppFoo* ptr
    cdef const cppFoo* constPtr

Now if I use the ptr in another class Foo 2 in a method, everything is alright. But if I use the constPtr from extern, it does not recognize it as a C++ object, but tells meI was using a python object?!? External call with void evaluateFoo(Foo &foo):

in Foo2:
    def evaluateFoo(self,  Foo foo):
        if (foo.isConstant()):
            #doesnt work
            return self.foo2ptr.evaluateFoo(deref(foo.constPtr))
        else:
            #works fine
            return self.foo2ptr.evaluateFoo(deref(foo.ptr))

Compiler: "Invalid operand type for '*' (Python object)"

What is wrong here?

Robert Bradshaw

unread,
Dec 5, 2016, 1:01:28 PM12/5/16
to cython...@googlegroups.com
The following code works fine for me:

from cython.operator cimport dereference as deref

cdef extern from *:
cdef cppclass cppFoo:
pass
cdef cppclass cppFoo2:
int evaluateFoo(cppFoo)

cdef class Foo:
cdef cppFoo* ptr
cdef const cppFoo* constPtr

cdef class Foo2:
cdef cppFoo2* foo2ptr
def evaluateFoo(self, Foo foo):
if (foo.isConstant()):
return self.foo2ptr.evaluateFoo(deref(foo.constPtr))
else:
return self.foo2ptr.evaluateFoo(deref(foo.ptr))
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to cython-users...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Sandra Hicks

unread,
Dec 5, 2016, 4:02:35 PM12/5/16
to cython-users
Thank you! I took a look at your code and I had a clue what was missing. I defined my C++ Interface in pxd files and my wrapper classes in extra pyx files, as they are rather large. I had a definition of the wrapper class also in the pxd file for making it available to other classes, but I only had the non-const pointer declared there as I added the const one later. After adding it in the pxd file it works.
Reply all
Reply to author
Forward
0 new messages