Skip to content

Virtual function dispatch has problems with similar-named functions #159

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

Closed
bennybp opened this issue Apr 8, 2016 · 0 comments
Closed

Comments

@bennybp
Copy link
Contributor

bennybp commented Apr 8, 2016

I am unable to call a virtual function (implemented in python) from that virtual function in another object. It looks like there is code in get_overload that compares function names and returns a null function if it matches. I suppose this was added to prevent infinite looping (?), but results in false positives.

Small example: Cpp File:

#include <iostream>
#include <pybind11/pybind11.h>

struct Base {
    virtual void Print(void) const = 0;
};

struct PyClass {

    virtual void Print(void) const {
        PYBIND11_OVERLOAD_PURE(
            void,
            Base,
            Print
        );
    }
};

void CallPrint(const Base * b) { b->Print(); }

PYBIND11_PLUGIN(mytest)
{
    pybind11::module m("mytest", "A test");

    pybind11::class_<PyClass> base(m, "Base");
    base.alias<Base>()
        .def(pybind11::init<>())
        .def("Print", &Base::Print);

    m.def("CallPrint", &CallPrint);

    return m.ptr();
}

Python:

import mytest as my

class PyClass1(my.Base):
  def __init__(self):
    my.Base.__init__(self)

  def Print(self):
    print("In PyClass1")


class PyClass2(my.Base):
  def __init__(self):
    my.Base.__init__(self)

  def Print(self):
    print("In PyClass2")
    p = PyClass1()
    my.CallPrint(p)  # <<< Problem here

b = PyClass2()
my.CallPrint(b)

Error is RuntimeError: Tried to call pure virtual function "Print"

@wjakob wjakob changed the title Calling virtual function from another with the same name Virtual function dispatch has problems with similar-named functions Apr 11, 2016
@wjakob wjakob closed this as completed in f5c154a Apr 11, 2016
sschnug pushed a commit to sschnug/pybind11 that referenced this issue Aug 2, 2024
* chore: update pre-commit hooks

updates:
- [github.com/psf/black-pre-commit-mirror: 23.9.1 → 23.10.1](psf/black-pre-commit-mirror@23.9.1...23.10.1)
- [github.com/astral-sh/ruff-pre-commit: v0.1.0 → v0.1.3](astral-sh/ruff-pre-commit@v0.1.0...v0.1.3)
- [github.com/pre-commit/mirrors-mypy: v1.6.0 → v1.6.1](pre-commit/mirrors-mypy@v1.6.0...v1.6.1)

* chore: move to ruff-format

Signed-off-by: Henry Schreiner <[email protected]>

---------

Signed-off-by: Henry Schreiner <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Henry Schreiner <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant