⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classes.html

📁 cpptcl library for ns2
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head>  <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">  <title>C++/Tcl</title></head><body><table style="width: 100%; text-align: left;" border="0" cellpadding="2" cellspacing="20">  <tbody>    <tr>      <td style="vertical-align: top;"><font size="+3"><span style="font-family: helvetica,arial,sans-serif; font-weight: bold; color: rgb(0, 0, 255);">C++/Tcl</span></font><br>      <span style="font-family: helvetica,arial,sans-serif; color: rgb(0, 0, 255); font-weight: bold;">AC++ library for interoperability between C++ and Tcl</span> </td>      <td style="vertical-align: top; text-align: right;"><br>      </td>    </tr>  </tbody></table><br>[<a href="freefun.html">prev</a>][<a href="index.html">top</a>][<a href="objects.html">next</a>]<br><h4>Exposing Classes<br></h4>Exposing classes is a little bit more involving, since there is morethan name that needs to be defined.<br>Let's suppose that we have the following C++ class:<br><br><div style="margin-left: 40px;"><code>class Person</code><br><code>{</code><br><code>public:</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; void setName(string const &amp;n) { name= n; }</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; string getName() { return name; }</code><br><code></code><br><code>private:</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; string name;</code><br><code>};</code><br><code></code></div><br>We can expose this class in some extension (or to the embeddedinterpreter) like this:<br><br><div style="margin-left: 40px;"><code>CPPTCL_MODULE(Mymodule, i)</code><br><code>{</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; i.class_&lt;Person&gt;("Person")</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.def("setName", &amp;Person::setName)</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.def("getName", &amp;Person::getName);</code><br><code>}</code><br><code></code></div><br>Note: The i parameter to the <code>CPPTCL_MODULE</code> macro is aname that youlater use to refer to the interpreter. You can choose whatever name youlike. The rest of the code is the same as if the following object wasdeclared in the C++ program:<br><br><div style="margin-left: 40px;"><code>interpreter i;</code><br></div><br>Anyway - as you see, the class is defined using the <code>class_</code>member function template, called on the interpreter object (it couldnot be named "class", since that is a keyword in C++).<br>The string name that is provided as a parameter to the <code>class_</code>function is a name that will be visible to the Tcl scripts - again, itdoes not have to be the name of the C++ class that you use in code.<br><br><h4><a name="members"></a>Member functions</h4>This special <code>class_</code> function returns an object that canbe used to define class member functions, in the same expression and ina chained way.<br>The above example is written in three lines just to make it morereadable, but it is a single C++ instruction:<br><br><div style="margin-left: 40px;"><code>i.class_&lt;Person&gt;("Person")</code><code>.def("setName",&amp;Person::setName)</code><code>.def("getName", &amp;Person::getName);</code><br></div><br>The rules for defining class member functions are the same as the rulesused for <a href="freefun.html">Exposing Free Functions</a>.<br><br>After the class is exposed, it can be used in the following way (let'ssuppose that the above was compiled to the shared library named <code>mymodule.so</code>):<br><br><div style="margin-left: 40px;"><code>% load ./mymodule.so<br>% set p [Person]<br>p0x807b790<br>% $p setName "Maciej"<br>% $p getName<br>Maciej<br>% <br></code></div><br>As you see, there is a <code>Person</code> command that creates andreturns a new object. This is simply a constructor of our class. Itreturns a name of the new object (here it is <code>p0x807b790 - </code>itmay be different on your machine or when you run the same programtwice; you should not attach any external meaning to this name) andthis name is immediately available as a new command, to use with memberfunctions.<br>For example, the expression:<br><br><div style="margin-left: 40px;"><code>% $p setName "Maciej"</code><br><code></code></div><code></code><br>calls the member function <code>setName</code> with the parameter <code>"Maciej"</code>,on the object whose name is stored in the variable <code>p</code>.<br>Later, the expression:<br><br><div style="margin-left: 40px;"><code>% $p getName</code><br></div><br>calls the member function <code>getName</code> without any parametersand it returns the <code>string</code> result of that call.<br><br><h4><a name="constructors"></a>Constructors</h4>In the above example, the class <code>Person</code> has noconstructors. Or, to be strict, it has a default constructor withoutparameters.<br>The <code>Person</code> class could be exposed also in the followingway:<br><br><div style="margin-left: 40px;"><code>i.class_&lt;Person&gt;("Person", <span style="font-weight: bold;">init&lt;&gt;()</span>)</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; .def("setName", &amp;Person::setName)</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; .def("getName", &amp;Person::getName)<span style="font-family: sans-serif;">;</span></code><br></div><br>As you see, there is a special <code>init</code> object provided. Itcan carry information about the expected parameters that are needed forconstructor, as in the following full example:<br><br><div style="margin-left: 40px;"><code>// example4.cc</code><br><code></code><br><code>#include "cpptcl.h"</code><br><code>#include &lt;string&gt;</code><br><code></code><br><code>using namespace std;</code><br><code>using namespace Tcl;</code><br><code></code><br><code>class Person</code><br><code>{</code><br><code>public:</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; <span style="font-weight: bold;">Person(stringconst &amp;n) : name(n) {}</span></code><br><code></code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; void setName(string const &amp;n) { name= n; }</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; string getName() { return name; }</code><br><code></code><br><code>private:</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; string name;</code><br><code>};</code><br><code></code><br><code></code><br><code>CPPTCL_MODULE(Mymodule, i)</code><br><code>{</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; i.class_&lt;Person&gt;("Person", <span style="font-weight: bold;">init&lt;string const &amp;&gt;()</span>)</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.def("setName", &amp;Person::setName)</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.def("getName", &amp;Person::getName);</code><br><code>}</code><br><code></code></div><br>The <code>init</code> object can bring with it information about thenumber and the types of parameters needed by the constructor.<br>Above, it declares that the constructor of class <code>Person</code>needs 1 parameter of type <code>string const &amp;</code>.<br>This means that we cannot use this exposed class from the Tcl scriptthe same way as before:<br><br><div style="margin-left: 40px;"><code>% set p [Person]</code><br><code>Too few arguments.</code><br><code>% </code><br><code></code></div><br>Instead, we have to provide required parameters:<br><br><div style="margin-left: 40px;"><code>% set p [Person "Maciej"]</code><br><code>p0x807b7c0</code><br><code>% $p getName</code><br><code>Maciej</code><br><code>% <br></code><code></code></div><br>Constructors with up to 9 parameters can be defined this way.<br><br>Another form that may be sometimes useful is:<br><br><div style="margin-left: 40px;"><code>i.class_&lt;Person&gt;("Person", <span style="font-weight: bold;">no_init</span>)</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; .def("setName", &amp;Person::setName)</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; .def("getName", &amp;Person::getName);</code><br><code></code></div><br><code></code>This basically means that the exposed class has noconstructors at all (or, to be strict, we do not want them to bevisible from Tcl).<br>Objects of such classes need to be created by separate factoryfunctions.<br><br><h4><a name="destructors"></a>Destructors</h4>There is additional "member function" that is automatically defined foreach class, which is used for destroying the object:<br><br><div style="margin-left: 40px;"><code>% $p -delete</code><br></div><br>Once this is done, the object itself is destroyed and the associatedcommand is removed from the interpreter.<br>This means that the name stored in the variable p cannot be used forexecuting member functions any more:<br><br><div style="margin-left: 40px;"><code>% $p getName</code><br><code>invalid command name "p0x807b790"</code><br><code>% </code><br><code></code></div><br><br>[<a href="freefun.html">prev</a>][<a href="index.html">top</a>][<a href="objects.html">next</a>]<br><br><hr style="width: 100%; height: 2px;">Copyright &copy; 2004-2006,MaciejSobczak<br><br></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -