-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Wrong object type with inheritance unless virtual function is present #645
Comments
Basically, you have to have at least one virtual method in order to have a proper polymorphic class. In practice, that's almost always the case anyway because you almost always want to have a virtual destructor ( |
Ok, if that's the case, then this would be a great thing to call out in the documentation in the inheritance section. |
This isn't really specific to pybind11. It applies to anything in C++: If you have an owned base pointer to a derived class, the base must have a virtual destructor. Otherwise, the behavior is undefined in C++. Pybind11 docs are pretty extensive as it is (it takes quite a while to read everything), so I'm not sure including C++ 101 topics is entirely useful. |
If the users of pybind11 were exclusively people who use C++ extensively, I might agree. However, I suspect there are more than a few users who don't use C++ on a daily basis but just need to wrap something so they can use it in python. Having a note for those users would be a good thing. |
I'm on Fedora 24 with GCC 6.3.1, Python 3.5, haven't tested on other platforms. Here's the test code:
I execute the following and get:
As you can imagine, this isn't quite what I expected. I played around a bit, and I discovered that when I added a virtual function to A, then this magically started working as I expected:
Is there something I'm missing here, or is this intended behavior?
The text was updated successfully, but these errors were encountered: