1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>wxWidgets: Runtime Type Information (RTTI)</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="extra_stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="page_container">
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0" style="width: 100%;">
<tbody>
<tr>
<td id="projectlogo">
<a href="http://www.wxwidgets.org/" target="_new">
<img alt="wxWidgets" src="logo.png"/>
</a>
</td>
<td style="padding-left: 0.5em; text-align: right;">
<span id="projectnumber">Version: 3.0.2</span>
</td>
</tr>
</tbody>
</table>
</div>
<!-- Generated by Doxygen 1.8.2 -->
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main Page</span></a></li>
<li class="current"><a href="pages.html"><span>Related Pages</span></a></li>
<li><a href="modules.html"><span>Categories</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li><a href="files.html"><span>Files</span></a></li>
</ul>
</div>
<div id="nav-path" class="navpath">
<ul>
<li class="navelem"><a class="el" href="index.html">Documentation</a></li><li class="navelem"><a class="el" href="page_topics.html">Programming Guides</a></li> </ul>
</div>
</div><!-- top -->
<div class="header">
<div class="headertitle">
<div class="title">Runtime Type Information (RTTI) </div> </div>
</div><!--header-->
<div class="contents">
<div class="toc"><h3>Table of Contents</h3>
<ul><li class="level1"><a href="#overview_rtti_classinfo">wxClassInfo</a></li>
<li class="level1"><a href="#overview_rtti_example">Example</a></li>
</ul>
</div>
<div class="textblock"><p>One of the failings of C++ used to be that no runtime information was provided about a class and its position in the inheritance hierarchy.</p>
<p>Another, which still persists, is that instances of a class cannot be created just by knowing the name of a class, which makes facilities such as persistent storage hard to implement.</p>
<p>Most C++ GUI frameworks overcome these limitations by means of a set of macros and functions and wxWidgets is no exception. As it originated before the addition of RTTI to the C++ standard and as support for it is still missing from some (albeit old) compilers, wxWidgets doesn't (yet) use it, but provides its own macro-based RTTI system.</p>
<p>In the future, the standard C++ RTTI will be used though and you're encouraged to use whenever possible the wxDynamicCast macro which, for the implementations that support it, is defined just as dynamic_cast and uses wxWidgets RTTI for all the others. This macro is limited to wxWidgets classes only and only works with pointers (unlike the real dynamic_cast which also accepts references).</p>
<p>Each class that you wish to be known to the type system should have a macro such as DECLARE_DYNAMIC_CLASS just inside the class declaration. The macro IMPLEMENT_DYNAMIC_CLASS should be in the implementation file. Note that these are entirely optional; use them if you wish to check object types, or create instances of classes using the class name. However, it is good to get into the habit of adding these macros for all classes.</p>
<p>Variations on these macros are used for multiple inheritance, and abstract classes that cannot be instantiated dynamically or otherwise.</p>
<p>DECLARE_DYNAMIC_CLASS inserts a static <a class="el" href="classwx_class_info.html" title="This class stores meta-information about classes.">wxClassInfo</a> declaration into the class, initialized by IMPLEMENT_DYNAMIC_CLASS. When initialized, the <a class="el" href="classwx_class_info.html" title="This class stores meta-information about classes.">wxClassInfo</a> object inserts itself into a linked list (accessed through wxClassInfo::first and wxClassInfo::next pointers). The linked list is fully created by the time all global initialisation is done.</p>
<p>IMPLEMENT_DYNAMIC_CLASS is a macro that not only initialises the static <a class="el" href="classwx_class_info.html" title="This class stores meta-information about classes.">wxClassInfo</a> member, but defines a global function capable of creating a dynamic object of the class in question. A pointer to this function is stored in <a class="el" href="classwx_class_info.html" title="This class stores meta-information about classes.">wxClassInfo</a>, and is used when an object should be created dynamically.</p>
<p><a class="el" href="classwx_object.html#af40d580385cf4f8112fae7713404b01e" title="Determines whether this class is a subclass of (or the same class as) the given class.">wxObject::IsKindOf</a> uses the linked list of <a class="el" href="classwx_class_info.html" title="This class stores meta-information about classes.">wxClassInfo</a>. It takes a <a class="el" href="classwx_class_info.html" title="This class stores meta-information about classes.">wxClassInfo</a> argument, so use CLASSINFO(className) to return an appropriate <a class="el" href="classwx_class_info.html" title="This class stores meta-information about classes.">wxClassInfo</a> pointer to use in this function.</p>
<p>The function wxCreateDynamicObject can be used to construct a new object of a given type, by supplying a string name. If you have a pointer to the <a class="el" href="classwx_class_info.html" title="This class stores meta-information about classes.">wxClassInfo</a> object instead, then you can simply call <a class="el" href="classwx_class_info.html#a754a267b65cd7e2026e6a0beb22ff0a7" title="Creates an object of the appropriate kind.">wxClassInfo::CreateObject</a>.</p>
<dl class="section see"><dt>See Also</dt><dd><a class="el" href="classwx_object.html" title="This is the root class of many of the wxWidgets classes.">wxObject</a></dd></dl>
<h1><a class="anchor" id="overview_rtti_classinfo"></a>
wxClassInfo</h1>
<p>This class stores meta-information about classes. An application may use macros such as DECLARE_DYNAMIC_CLASS and IMPLEMENT_DYNAMIC_CLASS to record runtime information about a class, including:</p>
<ul>
<li>Its position in the inheritance hierarchy. </li>
<li>The base class name(s) (up to two base classes are permitted). </li>
<li>A string representation of the class name. </li>
<li>A function that can be called to construct an instance of this class.</li>
</ul>
<p>The DECLARE_... macros declare a static <a class="el" href="classwx_class_info.html" title="This class stores meta-information about classes.">wxClassInfo</a> variable in a class, which is initialized by macros of the form IMPLEMENT_... in the implementation C++ file. Classes whose instances may be constructed dynamically are given a global constructor function which returns a new object.</p>
<p>You can get the <a class="el" href="classwx_class_info.html" title="This class stores meta-information about classes.">wxClassInfo</a> for a class by using the CLASSINFO macro, e.g. CLASSINFO(wxFrame). You can get the <a class="el" href="classwx_class_info.html" title="This class stores meta-information about classes.">wxClassInfo</a> for an object using <a class="el" href="classwx_object.html#ab3a0c6f723cbaddb47be4e8dd98cc8e2" title="This virtual function is redefined for every class that requires run-time type information, when using the wxDECLARE_CLASS macro (or similar).">wxObject::GetClassInfo</a>.</p>
<h1><a class="anchor" id="overview_rtti_example"></a>
Example</h1>
<p>In a header file <a class="el" href="frame_8h.html">frame.h</a>:</p>
<div class="fragment"><div class="line"><span class="keyword">class </span><a class="code" href="classwx_frame.html" title="A frame is a window whose size and position can (usually) be changed by the user.">wxFrame</a> : <span class="keyword">public</span> <a class="code" href="classwx_window.html" title="wxWindow is the base class for all windows and represents any visible object on screen.">wxWindow</a></div>
<div class="line">{</div>
<div class="line"> DECLARE_DYNAMIC_CLASS(<a class="code" href="classwx_frame.html" title="A frame is a window whose size and position can (usually) be changed by the user.">wxFrame</a>)</div>
<div class="line"></div>
<div class="line">private:</div>
<div class="line"> <a class="code" href="classwx_string.html" title="String class for passing textual data to or receiving it from wxWidgets.">wxString</a> m_title;</div>
<div class="line"></div>
<div class="line">public:</div>
<div class="line">...</div>
<div class="line">};</div>
</div><!-- fragment --><p>In a C++ file frame.cpp:</p>
<div class="fragment"><div class="line">IMPLEMENT_DYNAMIC_CLASS(<a class="code" href="classwx_frame.html" title="A frame is a window whose size and position can (usually) be changed by the user.">wxFrame</a>, <a class="code" href="classwx_window.html" title="wxWindow is the base class for all windows and represents any visible object on screen.">wxWindow</a>)</div>
<div class="line"></div>
<div class="line"><a class="code" href="classwx_frame.html" title="A frame is a window whose size and position can (usually) be changed by the user.">wxFrame</a>::<a class="code" href="classwx_frame.html" title="A frame is a window whose size and position can (usually) be changed by the user.">wxFrame</a>()</div>
<div class="line">{</div>
<div class="line">...</div>
<div class="line">}</div>
</div><!-- fragment --> </div></div><!-- contents -->
<address class="footer">
<small>
Generated on Thu Nov 27 2014 13:46:42 for wxWidgets by <a href="http://www.doxygen.org/index.html" target="_new">Doxygen</a> 1.8.2
</small>
</address>
<script src="wxwidgets.js" type="text/javascript"></script>
</div><!-- #page_container -->
</body>
</html>
|