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