minor text edits in advanced/classes.rst (unrelated to PR)

This commit is contained in:
Wenzel Jakob 2017-08-22 00:55:53 +02:00
parent 234f7c39a0
commit fb276c661f

View File

@ -381,12 +381,12 @@ class like this:
static Example create(int a) { return Example(a); } static Example create(int a) { return Example(a); }
}; };
While it is possible to expose the ``create`` method to Python, it is often While it is possible to create a straightforward binding of the static
preferrable to expose it on the Python side as a constructor rather than a ``create`` method, it may sometimes be preferable to expose it as a constructor
named static method. You can do this by calling ``.def(py::init(...))`` with on the Python side. This can be accomplished by calling ``.def(py::init(...))``
the function reference returning the new instance passed as an argument. It is with the function reference returning the new instance passed as an argument.
also possible to use this approach to bind a function returning a new instance It is also possible to use this approach to bind a function returning a new
by raw pointer or by the holder (e.g. ``std::unique_ptr``). instance by raw pointer or by the holder (e.g. ``std::unique_ptr``).
The following example shows the different approaches: The following example shows the different approaches:
@ -421,18 +421,20 @@ The following example shows the different approaches:
When the constructor is invoked from Python, pybind11 will call the factory When the constructor is invoked from Python, pybind11 will call the factory
function and store the resulting C++ instance in the Python instance. function and store the resulting C++ instance in the Python instance.
When combining factory functions constructors with :ref:`overriding_virtuals` When combining factory functions constructors with :ref:`virtual function
there are two approaches. The first is to add a constructor to the alias class trampolines <overriding_virtuals>` there are two approaches. The first is to
that takes a base value by rvalue-reference. If such a constructor is add a constructor to the alias class that takes a base value by
available, it will be used to construct an alias instance from the value rvalue-reference. If such a constructor is available, it will be used to
returned by the factory function. The second option is to provide two factory construct an alias instance from the value returned by the factory function.
functions to ``py::init()``: the first will be invoked when no alias class is The second option is to provide two factory functions to ``py::init()``: the
required (i.e. when the class is being used but not inherited from in Python), first will be invoked when no alias class is required (i.e. when the class is
and the second will be invoked when an alias is required. being used but not inherited from in Python), and the second will be invoked
when an alias is required.
You can also specify a single factory function that always returns an alias You can also specify a single factory function that always returns an alias
instance: this will result in behaviour similar to ``py::init_alias<...>()``, instance: this will result in behaviour similar to ``py::init_alias<...>()``,
as described in :ref:`extended_aliases`. as described in the :ref:`extended trampoline class documentation
<extended_aliases>`.
The following example shows the different factory approaches for a class with The following example shows the different factory approaches for a class with
an alias: an alias: