Commit 4af8faf2 authored by Robert Bradshaw's avatar Robert Bradshaw Committed by GitHub

Merge pull request #525 from empyrical/cpp-class-parent-methods

Allow C++ class methods to use base class' methods
parents 2c3c04b2 05c79eaf
...@@ -6302,7 +6302,7 @@ class AttributeNode(ExprNode): ...@@ -6302,7 +6302,7 @@ class AttributeNode(ExprNode):
# as an ordinary function. # as an ordinary function.
if entry.func_cname and not hasattr(entry.type, 'op_arg_struct'): if entry.func_cname and not hasattr(entry.type, 'op_arg_struct'):
cname = entry.func_cname cname = entry.func_cname
if entry.type.is_static_method: if entry.type.is_static_method or env.parent_scope.is_cpp_class_scope:
ctype = entry.type ctype = entry.type
elif type.is_cpp_class: elif type.is_cpp_class:
error(self.pos, "%s not a static member of %s" % (entry.name, type)) error(self.pos, "%s not a static member of %s" % (entry.name, type))
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
cdef double pi cdef double pi
from math import pi from math import pi
from libc.math cimport sin, cos from libc.math cimport sin, cos
from libcpp cimport bool
cdef extern from "shapes.h" namespace "shapes": cdef extern from "shapes.h" namespace "shapes":
cdef cppclass Shape: cdef cppclass Shape:
...@@ -42,6 +43,35 @@ def test_Poly(int n, float radius=1): ...@@ -42,6 +43,35 @@ def test_Poly(int n, float radius=1):
finally: finally:
del poly del poly
cdef cppclass BaseClass:
int n
int method():
return this.n
cdef cppclass SubClass(BaseClass):
bool override
__init__(bool override):
this.n = 1
this.override = override
int method():
if override:
return 0
else:
return BaseClass.method()
def test_BaseMethods(x):
"""
>>> test_BaseMethods(True)
0
>>> test_BaseMethods(False)
1
"""
cdef SubClass* subClass
try:
subClass = new SubClass(x)
return subClass.method()
finally:
del subClass
cdef cppclass WithStatic: cdef cppclass WithStatic:
@staticmethod @staticmethod
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment