mirror of
https://github.com/pybind/pybind11.git
synced 2025-01-19 01:15:52 +00:00
added note about cast operations
This commit is contained in:
parent
5cd3311c6c
commit
436b731891
@ -711,6 +711,23 @@ Available types include :class:`handle`, :class:`object`, :class:`bool_`,
|
||||
:class:`dict`, :class:`slice`, :class:`capsule`, :class:`function`,
|
||||
:class:`buffer`, :class:`array`, and :class:`array_t`.
|
||||
|
||||
In this kind of mixed code, it is often necessary to convert arbitrary C++
|
||||
types to Python, which can be done using :func:`cast`:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
MyClass *cls = ..;
|
||||
py::object obj = py::cast(cls);
|
||||
|
||||
The reverse direction uses the following syntax:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
py::object obj = ...;
|
||||
MyClass *cls = obj.cast<MyClass *>();
|
||||
|
||||
When conversion fails, both directions throw the exception :class:`cast_error`.
|
||||
|
||||
.. seealso::
|
||||
|
||||
The file :file:`example/example2.cpp` contains a complete example that
|
||||
|
Loading…
Reference in New Issue
Block a user