You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
I have a simple two class hierarchy, Foo which derives from Bar. In Bar I define a method to_str meant to print Bar. In Foo I try to override to_str so that it first calls Bar::to_str then prints Foo's additional state; instead I get infinite recursion.
To Reproduce
MWE:
cpp_class(Bar)
cpp_constructor(CTOR Bar str)
function("${CTOR}" self _ctor_value)
Bar(SET"${self}" _value "${_ctor_value}")
endfunction()
cpp_member(to_str Bar desc)
function("${to_str}" self _ts_result)
Bar(GET"${self}" _ts_value "_value")
set("${_ts_result}""Value = ${_ts_value}"PARENT_SCOPE)
endfunction()
cpp_attr(Bar _value)
cpp_end_class()
cpp_class(Foo Bar)
cpp_constructor(CTOR Foo str)
function("${CTOR}" self _ctor_value)
# Related, is possible to call the base ctor?
Foo(SET"${self}" _other_value "${_ctor_value}")
endfunction()
cpp_member(to_str Foo desc)
function("${to_str}" self _ts_result)
Bar(to_str "${self}" _ts_base)
Foo(GET"${self}" _ts_value "_other_value")
set(
"${_ts_result}""${_ts_base}\nOther Value = ${_ts_value}"PARENT_SCOPE
)
endfunction()
cpp_attr(Foo _other_value)
cpp_end_class()
Bar(CTOR my_bar "hello")
Bar(to_str "${my_bar}" as_a_string)
message("${as_a_string}") # Prints "Value = hello"
Foo(CTOR my_foo "world")
Foo(to_str "${my_foo}" as_a_string)
message("${as_a_string}") #Crashes here
Expected behavior
I would expect the code to print "Value = \nOther Value = world"
Additional context
N/A
The text was updated successfully, but these errors were encountered:
I think we would need to introduce a super() construct for this to work. Currently, to support polymorphism the subclass completely replaces the vtable entry (terminology for other languages, don't remember what we call it) for the to_str method so that you can pass a Foo into something expecting a Bar and still get the overridden functionality.
Describe the bug
I have a simple two class hierarchy,
Foo
which derives fromBar
. InBar
I define a methodto_str
meant to printBar
. InFoo
I try to overrideto_str
so that it first callsBar::to_str
then printsFoo
's additional state; instead I get infinite recursion.To Reproduce
MWE:
Expected behavior
I would expect the code to print
"Value = \nOther Value = world"
Additional context
N/A
The text was updated successfully, but these errors were encountered: