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