]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/truntime.tex
wxChoice/wxComboBox background colour change (to get back to where we were);
[wxWidgets.git] / docs / latex / wx / truntime.tex
CommitLineData
a660d684
KB
1\section{Run time class information overview}\label{runtimeclassoverview}
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
34636400
VZ
12macros and functions and wxWindows is no exception. As it originated before the
13addition of RTTI to the standard C++ and as support for it still missing from
14some (albeit old) compilers, wxWindows doesn't (yet) use it, but provides its
15own macro-based RTTI system.
16
17In the future, the standard C++ RTTI will be used though and you're encouraged
18to use whenever possible \helpref{wxDynamicCast()}{wxdynamiccast} macro which,
19for the implementations that support it, is defined just as dynamic\_cast<> and
20uses wxWindows RTTI for all the others. This macro is limited to wxWindows
21classes only and only works with pointers (unlike the real dynamic\_cast<> which
22also accepts referencies).
23
a660d684
KB
24Each class that you wish to be known the type system should have
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
KB
30
31Variations on these \helpref{macros}{macros} are used for multiple inheritance, and abstract
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
53can simply call wxClassInfo::CreateObject.
54
55\subsection{wxClassInfo}\label{wxclassinfooverview}
56
57\overview{Run time class information overview}{runtimeclassoverview}
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
81\subsection{Example}
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