📄 node25.html
字号:
<html><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><!--Converted with jLaTeX2HTML 2002 (1.62) JA patch-1.4patched version by: Kenshi Muto, Debian Project.LaTeX2HTML 2002 (1.62),original version by: Nikos Drakos, CBLU, University of Leeds* revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan* with significant contributions from: Jens Lippmann, Marek Rouchal, Martin Wilck and others --><HTML><HEAD><TITLE>3.5.1 How to Bind Static C++ Class Member Variables</TITLE><META NAME="description" CONTENT="3.5.1 How to Bind Static C++ Class Member Variables"><META NAME="keywords" CONTENT="everything"><META NAME="resource-type" CONTENT="document"><META NAME="distribution" CONTENT="global"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"><META NAME="Generator" CONTENT="jLaTeX2HTML v2002 JA patch-1.4"><META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css"><LINK REL="STYLESHEET" HREF="everything.css" tppabs="http://www.isi.edu/nsnam/ns/doc/everything.css"><LINK REL="previous" HREF="node24.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node24.html"><LINK REL="up" HREF="node24.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node24.html"><LINK REL="next" HREF="node26.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node26.html"></HEAD><BODY ><!--Navigation Panel--><A NAME="tex2html1507" HREF="node26.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node26.html"><IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="file:/usr/share/latex2html/icons/next.png"></A> <A NAME="tex2html1501" HREF="node24.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node24.html"><IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="file:/usr/share/latex2html/icons/up.png"></A> <A NAME="tex2html1497" HREF="node24.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node24.html"><IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="file:/usr/share/latex2html/icons/prev.png"></A> <A NAME="tex2html1503" HREF="node1.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node1.html"><IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="file:/usr/share/latex2html/icons/contents.png"></A> <A NAME="tex2html1505" HREF="node590.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node590.html"><IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="file:/usr/share/latex2html/icons/index.png"></A> <BR><B> Next:</B> <A NAME="tex2html1508" HREF="node26.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node26.html">3.6 Class TclCommand</A><B> Up:</B> <A NAME="tex2html1502" HREF="node24.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node24.html">3.5 Class TclClass</A><B> Previous:</B> <A NAME="tex2html1498" HREF="node24.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node24.html">3.5 Class TclClass</A>   <B> <A NAME="tex2html1504" HREF="node1.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node1.html">Contents</A></B>   <B> <A NAME="tex2html1506" HREF="node590.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node590.html">Index</A></B> <BR><BR><!--End of Navigation Panel--><H2><A NAME="SECTION02151000000000000000">3.5.1 How to Bind Static C++ Class Member Variables</A></H2><P>In Section <A HREF="node15.html#sec:TclObject" tppabs="http://www.isi.edu/nsnam/ns/doc/node15.html#sec:TclObject">3.4</A>, we have seen how to expose membervariables of a C++ object into OTcl space. This, however, does not apply to static member variables of a C++class. Of course, one may create an OTcl variable for the static membervariable of every C++ object; obviously this defeats the whole meaningof static members.<P>We cannot solve this binding problem using a similar solution asbinding in TclObject, which is based on InstVar, because InstVars inTclCL require the presence of a TclObject.However, we can create a method of the corresponding TclClass andaccess static members of a C++ class through the methods of itscorresponding TclClass.The procedure is as follows:<OL><LI>Create your own derived TclClass as described above;</LI><LI>Declare methods []bind and []method in your derived class;</LI><LI>Create your binding methods in the implementation of your []bind with <TT>add_method("your_method")</TT>, then implement the handler in []method in a similar way as you would do in []TclObject::command. Notice that the number of arguments passed to []TclClass::method are different from those passed to []TclObject::command. The former has two more arguments in the front. </LI></OL><P>As an example, we show a simplified version of<TT>PacketHeaderClass</TT> in packet.cc. Suppose we have the following class <TT>Packet</TT> which has a staticvariable <TT>hdrlen_</TT> that we want to access from OTcl:<PRE>class Packet { ...... static int hdrlen_;};</PRE>Then we do the following to construct an accessor for this variable:<PRE>class PacketHeaderClass : public TclClass {protected: PacketHeaderClass(const char* classname, int hdrsize); TclObject* create(int argc, const char*const* argv); /* These two implements OTcl class access methods / virtual void bind(); virtual int method(int argc, const char*const* argv);};void PacketHeaderClass::bind(){ /* Call to base class bind() must precede add_method() / TclClass::bind(); add_method("hdrlen");}int PacketHeaderClass::method(int ac, const char*const* av){ Tcl& tcl = Tcl::instance(); \* Notice this argument translation; we can then handle them \as if in TclObject::command() */ int argc = ac - 2; const char*const* argv = av + 2; if (argc == 2) { if (strcmp(argv[1], "hdrlen") == 0) { tcl.resultf("%d", Packet::hdrlen_); return (TCL_OK); } } else if (argc == 3) { if (strcmp(argv[1], "hdrlen") == 0) { Packet::hdrlen_ = atoi(argv[2]); return (TCL_OK); } } return TclClass::method(ac, av);}</PRE>After this, we can then use the following OTcl command to access andchange values of <TT>Packet::hdrlen_</TT>:<PRE> PacketHeader hdrlen 120 set i [PacketHeader hdrlen]</PRE><P><HR><!--Navigation Panel--><A NAME="tex2html1507" HREF="node26.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node26.html"><IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="file:/usr/share/latex2html/icons/next.png"></A> <A NAME="tex2html1501" HREF="node24.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node24.html"><IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="file:/usr/share/latex2html/icons/up.png"></A> <A NAME="tex2html1497" HREF="node24.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node24.html"><IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="file:/usr/share/latex2html/icons/prev.png"></A> <A NAME="tex2html1503" HREF="node1.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node1.html"><IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="file:/usr/share/latex2html/icons/contents.png"></A> <A NAME="tex2html1505" HREF="node590.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node590.html"><IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="file:/usr/share/latex2html/icons/index.png"></A> <BR><B> Next:</B> <A NAME="tex2html1508" HREF="node26.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node26.html">3.6 Class TclCommand</A><B> Up:</B> <A NAME="tex2html1502" HREF="node24.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node24.html">3.5 Class TclClass</A><B> Previous:</B> <A NAME="tex2html1498" HREF="node24.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node24.html">3.5 Class TclClass</A>   <B> <A NAME="tex2html1504" HREF="node1.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node1.html">Contents</A></B>   <B> <A NAME="tex2html1506" HREF="node590.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node590.html">Index</A></B> <!--End of Navigation Panel--><ADDRESS>2003-09-23</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -