mirror of
https://github.com/boostorg/python.git
synced 2026-01-22 17:32:55 +00:00
99 lines
6.1 KiB
HTML
99 lines
6.1 KiB
HTML
<html>
|
|
<head>
|
|
<!-- Generated by the Spirit (http://spirit.sf.net) QuickDoc -->
|
|
<title>Inheritance</title>
|
|
<link rel="stylesheet" href="theme/style.css" type="text/css">
|
|
<link rel="prev" href="class_properties.html">
|
|
<link rel="next" href="class_virtual_functions.html">
|
|
</head>
|
|
<body>
|
|
<table width="100%" height="48" border="0" cellspacing="2">
|
|
<tr>
|
|
<td><img src="theme/c%2B%2Bboost.gif">
|
|
</td>
|
|
<td width="85%">
|
|
<font size="6" face="Verdana, Arial, Helvetica, sans-serif"><b>Inheritance</b></font>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
<br>
|
|
<table border="0">
|
|
<tr>
|
|
<td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
|
|
<td width="30"><a href="class_properties.html"><img src="theme/l_arr.gif" border="0"></a></td>
|
|
<td width="20"><a href="class_virtual_functions.html"><img src="theme/r_arr.gif" border="0"></a></td>
|
|
</tr>
|
|
</table>
|
|
<p>
|
|
In the previous examples, we dealt with classes that are not polymorphic.
|
|
This is not often the case. Much of the time, we will be wrapping
|
|
polymorphic classes and class hierarchies related by inheritance. We will
|
|
often have to write Boost.Python wrappers for classes that are derived from
|
|
abstract base classes.</p>
|
|
<p>
|
|
Consider this trivial inheritance structure:</p>
|
|
<code><pre>
|
|
<span class=keyword>struct </span><span class=identifier>Base </span><span class=special>{ </span><span class=keyword>virtual </span><span class=special>~</span><span class=identifier>Base</span><span class=special>(); </span><span class=special>};
|
|
</span><span class=keyword>struct </span><span class=identifier>Derived </span><span class=special>: </span><span class=identifier>Base </span><span class=special>{};
|
|
</span></pre></code>
|
|
<p>
|
|
And a set of C++ functions operating on <tt>Base</tt> and <tt>Derived</tt> object
|
|
instances:</p>
|
|
<code><pre>
|
|
<span class=keyword>void </span><span class=identifier>b</span><span class=special>(</span><span class=identifier>Base</span><span class=special>*);
|
|
</span><span class=keyword>void </span><span class=identifier>d</span><span class=special>(</span><span class=identifier>Derived</span><span class=special>*);
|
|
</span><span class=identifier>Base</span><span class=special>* </span><span class=identifier>factory</span><span class=special>() </span><span class=special>{ </span><span class=keyword>return </span><span class=keyword>new </span><span class=identifier>Derived</span><span class=special>; </span><span class=special>}
|
|
</span></pre></code>
|
|
<p>
|
|
We've seen how we can wrap the base class <tt>Base</tt>:</p>
|
|
<code><pre>
|
|
<span class=identifier>class_</span><span class=special><</span><span class=identifier>Base</span><span class=special>>(</span><span class=string>"Base"</span><span class=special>)
|
|
</span><span class=comment>/*...*/
|
|
</span><span class=special>;
|
|
</span></pre></code>
|
|
<p>
|
|
Now we can inform Boost.Python of the inheritance relationship between
|
|
<tt>Derived</tt> and its base class <tt>Base</tt>. Thus:</p>
|
|
<code><pre>
|
|
<span class=identifier>class_</span><span class=special><</span><span class=identifier>Derived</span><span class=special>, </span><span class=identifier>bases</span><span class=special><</span><span class=identifier>Base</span><span class=special>> </span><span class=special>>(</span><span class=string>"Derived"</span><span class=special>)
|
|
</span><span class=comment>/*...*/
|
|
</span><span class=special>;
|
|
</span></pre></code>
|
|
<p>
|
|
Doing so, we get some things for free:</p>
|
|
<ol><li>Derived automatically inherits all of Base's Python methods (wrapped C++ member functions)</li><li><b>If</b> Base is polymorphic, <tt>Derived</tt> objects which have been passed to Python via a pointer or reference to <tt>Base</tt> can be passed where a pointer or reference to <tt>Derived</tt> is expected.</li></ol><p>
|
|
Now, we shall expose the C++ free functions <tt>b</tt> and <tt>d</tt> and <tt>factory</tt>:</p>
|
|
<code><pre>
|
|
<span class=identifier>def</span><span class=special>(</span><span class=string>"b"</span><span class=special>, </span><span class=identifier>b</span><span class=special>);
|
|
</span><span class=identifier>def</span><span class=special>(</span><span class=string>"d"</span><span class=special>, </span><span class=identifier>d</span><span class=special>);
|
|
</span><span class=identifier>def</span><span class=special>(</span><span class=string>"factory"</span><span class=special>, </span><span class=identifier>factory</span><span class=special>);
|
|
</span></pre></code>
|
|
<p>
|
|
Note that free function <tt>factory</tt> is being used to generate new
|
|
instances of class <tt>Derived</tt>. In such cases, we use
|
|
<tt>return_value_policy<manage_new_object></tt> to instruct Python to adopt
|
|
the pointer to <tt>Base</tt> and hold the instance in a new Python <tt>Base</tt>
|
|
object until the the Python object is destroyed. We shall see more of
|
|
Boost.Python <a href="call_policies.html">
|
|
call policies</a> later.</p>
|
|
<code><pre>
|
|
<span class=comment>// Tell Python to take ownership of factory's result
|
|
</span><span class=identifier>def</span><span class=special>(</span><span class=string>"factory"</span><span class=special>, </span><span class=identifier>factory</span><span class=special>,
|
|
</span><span class=identifier>return_value_policy</span><span class=special><</span><span class=identifier>manage_new_object</span><span class=special>>());
|
|
</span></pre></code>
|
|
<table border="0">
|
|
<tr>
|
|
<td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
|
|
<td width="30"><a href="class_properties.html"><img src="theme/l_arr.gif" border="0"></a></td>
|
|
<td width="20"><a href="class_virtual_functions.html"><img src="theme/r_arr.gif" border="0"></a></td>
|
|
</tr>
|
|
</table>
|
|
<br>
|
|
<hr size="1"><p class="copyright">Copyright © 2002 David Abrahams<br>Copyright © 2002 Joel de Guzman<br><br>
|
|
<font size="2">Permission to copy, use, modify, sell and distribute this document
|
|
is granted provided this copyright notice appears in all copies. This document
|
|
is provided "as is" without express or implied warranty, and with
|
|
no claim as to its suitability for any purpose. </font> </p>
|
|
</body>
|
|
</html>
|