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