Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
pybind11
Commits
7c0e2c24
Commit
7c0e2c24
authored
Jul 22, 2017
by
Dustin Spicuzza
Committed by
Dean Moldovan
Jul 23, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Document automatic upcasting of polymorphic types (#654)
Resolves #645.
parent
2e37fe09
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
61 additions
and
2 deletions
+61
-2
docs/classes.rst
+61
-2
No files found.
docs/classes.rst
View file @
7c0e2c24
...
@@ -225,8 +225,8 @@ just brings them on par.
...
@@ -225,8 +225,8 @@ just brings them on par.
.. _inheritance:
.. _inheritance:
Inheritance
Inheritance
and automatic upcasting
===========
===========
========================
Suppose now that the example consists of two data structures with an
Suppose now that the example consists of two data structures with an
inheritance relationship:
inheritance relationship:
...
@@ -283,6 +283,65 @@ expose fields and methods of both types:
...
@@ -283,6 +283,65 @@ expose fields and methods of both types:
>>> p.bark()
>>> p.bark()
u'woof!'
u'woof!'
The C++ classes defined above are regular non-polymorphic types with an
inheritance relationship. This is reflected in Python:
.. code-block:: cpp
// Return a base pointer to a derived instance
m.def("pet_store", []() { return std::unique_ptr<Pet>(new Dog("Molly")); });
.. code-block:: pycon
>>> p = example.pet_store()
>>> type(p) # `Dog` instance behind `Pet` pointer
Pet # no pointer upcasting for regular non-polymorphic types
>>> p.bark()
AttributeError: 'Pet' object has no attribute 'bark'
The function returned a ``Dog`` instance, but because it's a non-polymorphic
type behind a base pointer, Python only sees a ``Pet``. In C++, a type is only
considered polymorphic if it has at least one virtual function and pybind11
will automatically recognize this:
.. code-block:: cpp
struct PolymorphicPet {
virtual ~PolymorphicPet() = default;
};
struct PolymorphicDog : PolymorphicPet {
std::string bark() const { return "woof!"; }
};
// Same binding code
py::class_<PolymorphicPet>(m, "PolymorphicPet");
py::class_<PolymorphicDog, PolymorphicPet>(m, "PolymorphicDog")
.def(py::init<>())
.def("bark", &PolymorphicDog::bark);
// Again, return a base pointer to a derived instance
m.def("pet_store2", []() { return std::unique_ptr<PolymorphicPet>(new PolymorphicDog); });
.. code-block:: pycon
>>> p = example.pet_store2()
>>> type(p)
PolymorphicDog # automatically upcast
>>> p.bark()
u'woof!'
Given a pointer to a polymorphic base, pybind11 performs automatic upcasting
to the actual derived type. Note that this goes beyond the usual situation in
C++: we don't just get access to the virtual functions of the base, we get the
concrete derived type including functions and attributes that the base type may
not even be aware of.
.. seealso::
For more information about polymorphic behavior see :ref:`overriding_virtuals`.
Overloaded methods
Overloaded methods
==================
==================
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment