]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/truntime.tex
Applied ref-couting patch.
[wxWidgets.git] / docs / latex / wx / truntime.tex
CommitLineData
3980000c 1\section{Runtime class information (aka RTTI) overview}\label{runtimeclassoverview}
a660d684
KB
2
3Classes: \helpref{wxObject}{wxobject}, \helpref{wxClassInfo}{wxclassinfo}.
4
34636400 5One of the failings of C++ used to be that no run-time information was provided
a660d684 6about a class and its position in the inheritance hierarchy.
34636400
VZ
7Another, which still persists, is that instances of a class cannot be created
8just by knowing the name of a class, which makes facilities such as persistent
9storage hard to implement.
a660d684
KB
10
11Most C++ GUI frameworks overcome these limitations by means of a set of
fc2171bd 12macros and functions and wxWidgets is no exception. As it originated before the
3980000c 13addition of RTTI to the C++ standard and as support for it is still missing from
fc2171bd 14some (albeit old) compilers, wxWidgets doesn't (yet) use it, but provides its
34636400
VZ
15own macro-based RTTI system.
16
17In the future, the standard C++ RTTI will be used though and you're encouraged
f70c0443 18to use whenever possible the \helpref{wxDynamicCast()}{wxdynamiccast} macro which,
34636400 19for the implementations that support it, is defined just as dynamic\_cast<> and
fc2171bd 20uses wxWidgets RTTI for all the others. This macro is limited to wxWidgets
34636400 21classes only and only works with pointers (unlike the real dynamic\_cast<> which
f6bcfd97 22also accepts references).
34636400 23
f70c0443 24Each class that you wish to be known to the type system should have
a660d684
KB
25a macro such as DECLARE\_DYNAMIC\_CLASS just inside the class declaration.
26The macro IMPLEMENT\_DYNAMIC\_CLASS should be in the implementation file.
6b037754
JS
27Note that these are entirely optional; use them if you wish to check object
28types, or create instances of classes using the class name. However,
29it is good to get into the habit of adding these macros for all classes.
a660d684 30
f4fcc291 31Variations on these \helpref{macros}{rttimacros} are used for multiple inheritance, and abstract
a660d684
KB
32classes that cannot be instantiated dynamically or otherwise.
33
34DECLARE\_DYNAMIC\_CLASS inserts a static wxClassInfo declaration into the
35class, initialized by IMPLEMENT\_DYNAMIC\_CLASS. When initialized, the
36wxClassInfo object inserts itself into a linked list (accessed through
37wxClassInfo::first and wxClassInfo::next pointers). The linked list
38is fully created by the time all global initialisation is done.
39
40IMPLEMENT\_DYNAMIC\_CLASS is a macro that not only initialises the static
41wxClassInfo member, but defines a global function capable of creating a
42dynamic object of the class in question. A pointer to this function is
43stored in wxClassInfo, and is used when an object should be created
44dynamically.
45
34636400
VZ
46\helpref{wxObject::IsKindOf}{wxobjectiskindof} uses the linked list of
47wxClassInfo. It takes a wxClassInfo argument, so use CLASSINFO(className)
48to return an appropriate wxClassInfo pointer to use in this function.
a660d684
KB
49
50The function \helpref{wxCreateDynamicObject}{wxcreatedynamicobject} can be used
51to construct a new object of a given type, by supplying a string name.
52If you have a pointer to the wxClassInfo object instead, then you
f70c0443 53can simply call \helpref{wxClassInfo::CreateObject}{wxclassinfocreateobject}.
a660d684
KB
54
55\subsection{wxClassInfo}\label{wxclassinfooverview}
56
3980000c 57\overview{Runtime class information (aka RTTI) overview}{runtimeclassoverview}
a660d684
KB
58
59Class: \helpref{wxClassInfo}{wxclassinfo}
60
61This class stores meta-information about classes. An application
62may use macros such as DECLARE\_DYNAMIC\_CLASS and IMPLEMENT\_DYNAMIC\_CLASS
63to record run-time information about a class, including:
64
65\begin{itemize}\itemsep=0pt
66\item its position in the inheritance hierarchy;
67\item the base class name(s) (up to two base classes are permitted);
68\item a string representation of the class name;
69\item a function that can be called to construct an instance of this class.
70\end{itemize}
71
72The DECLARE\_... macros declare a static wxClassInfo variable in a class, which is initialized
73by macros of the form IMPLEMENT\_... in the implementation C++ file. Classes whose instances may be
74constructed dynamically are given a global constructor function which returns a new object.
75
76You can get the wxClassInfo for a class by using the CLASSINFO macro, e.g. CLASSINFO(wxFrame).
77You can get the wxClassInfo for an object using wxObject::GetClassInfo.
78
79See also \helpref{wxObject}{wxobject} and \helpref{wxCreateDynamicObject}{wxcreatedynamicobject}.
80
a203f6c0 81\subsection{Example}\label{runtimeclassinformationexample}
a660d684 82
34636400 83In a header file frame.h:
a660d684
KB
84
85\begin{verbatim}
34636400 86class wxFrame : public wxWindow
a660d684 87{
34636400
VZ
88DECLARE_DYNAMIC_CLASS(wxFrame)
89
90private:
91 wxString m_title;
a660d684 92
34636400
VZ
93public:
94 ...
a660d684
KB
95};
96\end{verbatim}
97
34636400 98In a C++ file frame.cpp:
a660d684
KB
99
100\begin{verbatim}
101IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxWindow)
102
34636400 103wxFrame::wxFrame()
a660d684
KB
104{
105...
106}
107\end{verbatim}
108
109