]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/truntime.tex
2nd attempt at MDI in wxMotif, using wxNotebook this time (still some probs).
[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
5One of the failings of C++ is that no run-time information is provided
6about a class and its position in the inheritance hierarchy.
7Another is that instances of a class cannot be created just by knowing the name of a class,
8which makes facilities such as persistent storage hard to implement.
9
10Most C++ GUI frameworks overcome these limitations by means of a set of
6b037754 11macros and functions and wxWindows is no exception.
a660d684
KB
12Each class that you wish to be known the type system should have
13a macro such as DECLARE\_DYNAMIC\_CLASS just inside the class declaration.
14The macro IMPLEMENT\_DYNAMIC\_CLASS should be in the implementation file.
6b037754
JS
15Note that these are entirely optional; use them if you wish to check object
16types, or create instances of classes using the class name. However,
17it is good to get into the habit of adding these macros for all classes.
a660d684
KB
18
19Variations on these \helpref{macros}{macros} are used for multiple inheritance, and abstract
20classes that cannot be instantiated dynamically or otherwise.
21
22DECLARE\_DYNAMIC\_CLASS inserts a static wxClassInfo declaration into the
23class, initialized by IMPLEMENT\_DYNAMIC\_CLASS. When initialized, the
24wxClassInfo object inserts itself into a linked list (accessed through
25wxClassInfo::first and wxClassInfo::next pointers). The linked list
26is fully created by the time all global initialisation is done.
27
28IMPLEMENT\_DYNAMIC\_CLASS is a macro that not only initialises the static
29wxClassInfo member, but defines a global function capable of creating a
30dynamic object of the class in question. A pointer to this function is
31stored in wxClassInfo, and is used when an object should be created
32dynamically.
33
34wxObject::IsKindOf uses the linked list of wxClassInfo. It takes
35a wxClassInfo argument, so use CLASSINFO(className) to return an
36appropriate wxClassInfo pointer to use in this function.
37
38The function \helpref{wxCreateDynamicObject}{wxcreatedynamicobject} can be used
39to construct a new object of a given type, by supplying a string name.
40If you have a pointer to the wxClassInfo object instead, then you
41can simply call wxClassInfo::CreateObject.
42
43\subsection{wxClassInfo}\label{wxclassinfooverview}
44
45\overview{Run time class information overview}{runtimeclassoverview}
46
47Class: \helpref{wxClassInfo}{wxclassinfo}
48
49This class stores meta-information about classes. An application
50may use macros such as DECLARE\_DYNAMIC\_CLASS and IMPLEMENT\_DYNAMIC\_CLASS
51to record run-time information about a class, including:
52
53\begin{itemize}\itemsep=0pt
54\item its position in the inheritance hierarchy;
55\item the base class name(s) (up to two base classes are permitted);
56\item a string representation of the class name;
57\item a function that can be called to construct an instance of this class.
58\end{itemize}
59
60The DECLARE\_... macros declare a static wxClassInfo variable in a class, which is initialized
61by macros of the form IMPLEMENT\_... in the implementation C++ file. Classes whose instances may be
62constructed dynamically are given a global constructor function which returns a new object.
63
64You can get the wxClassInfo for a class by using the CLASSINFO macro, e.g. CLASSINFO(wxFrame).
65You can get the wxClassInfo for an object using wxObject::GetClassInfo.
66
67See also \helpref{wxObject}{wxobject} and \helpref{wxCreateDynamicObject}{wxcreatedynamicobject}.
68
69\subsection{Example}
70
71In a header file wx\_frame.h:
72
73\begin{verbatim}
74class wxFrame: public wxWindow
75{
76 DECLARE_DYNAMIC_CLASS(wxFrame)
77
78 private:
79 char *frameTitle;
80 public:
81 ...
82};
83\end{verbatim}
84
85In a C++ file wx\_frame.cc:
86
87\begin{verbatim}
88IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxWindow)
89
90wxFrame::wxFrame(void)
91{
92...
93}
94\end{verbatim}
95
96