Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't Call Base Class's Methods #125

Open
ryanmrichard opened this issue Oct 30, 2023 · 2 comments
Open

Can't Call Base Class's Methods #125

ryanmrichard opened this issue Oct 30, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@ryanmrichard
Copy link
Contributor

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

@ryanmrichard ryanmrichard added the bug Something isn't working label Oct 30, 2023
@AutonomicPerfectionist
Copy link
Contributor

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.

@ryanmrichard
Copy link
Contributor Author

I think introducing super() to solve this is a great idea!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants