File: wx304.htm

package info (click to toggle)
wxwin2-doc 2.01-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 6,540 kB
  • ctags: 5,968
  • sloc: cpp: 15,157; makefile: 434; sh: 6
file content (97 lines) | stat: -rw-r--r-- 4,483 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<HTML>
<head><title>Run time class information overview</title></head>

<BODY BGCOLOR=#FFFFFF>
<A NAME="runtimeclassoverview"></A><CENTER>
<A HREF="wx.htm"><img align=center src="contents.gif" BORDER=0 ALT="Contents"></A> <A HREF="wx278.htm#overviews"><img align=center src="up.gif" BORDER=0 ALT="Up"></A> <A HREF="wx303.htm#resourceformats"><img align=center src="back.gif" BORDER=0 ALT="Previous"></A> <A HREF="wx305.htm#windowstyles"><img align=center src="forward.gif" BORDER=0 ALT="Next"></A> </CENTER><HR>

<H2>Run time class information overview</H2>
<P>
Classes: <A HREF="wx158.htm#wxobject">wxObject</A>, <A HREF="wx42.htm#wxclassinfo">wxClassInfo</A>.<P>
One of the failings of C++ is that no run-time information is provided
about a class and its position in the inheritance hierarchy.
Another is that instances of a class cannot be created just by knowing the name of a class,
which makes facilities such as persistent storage hard to implement.<P>
Most C++ GUI frameworks overcome these limitations by means of a set of
macros and functions and wxWindows is no exception.
Each class that you wish to be known the type system should have
a macro such as DECLARE_DYNAMIC_CLASS just inside the class declaration.
The macro IMPLEMENT_DYNAMIC_CLASS should be in the implementation file.
Note that these are entirely optional; use them if you wish to check object
types, or create instances of classes using the class name. However,
it is good to get into the habit of adding these macros for all classes.<P>
Variations on these <A HREF="wx272.htm#macros">macros</A> are used for multiple inheritance, and abstract
classes that cannot be instantiated dynamically or otherwise.<P>
DECLARE_DYNAMIC_CLASS inserts a static wxClassInfo declaration into the
class, initialized by IMPLEMENT_DYNAMIC_CLASS. When initialized, the
wxClassInfo object inserts itself into a linked list (accessed through
wxClassInfo::first and wxClassInfo::next pointers). The linked list
is fully created by the time all global initialisation is done.<P>
IMPLEMENT_DYNAMIC_CLASS is a macro that not only initialises the static
wxClassInfo member, but defines a global function capable of creating a
dynamic object of the class in question. A pointer to this function is
stored in wxClassInfo, and is used when an object should be created
dynamically.<P>
wxObject::IsKindOf uses the linked list of wxClassInfo. It takes
a wxClassInfo argument, so use CLASSINFO(className) to return an
appropriate wxClassInfo pointer to use in this function.<P>
The function <A HREF="wx271.htm#wxcreatedynamicobject">wxCreateDynamicObject</A> can be used
to construct a new object of a given type, by supplying a string name.
If you have a pointer to the wxClassInfo object instead, then you
can simply call wxClassInfo::CreateObject.<P>
<A HREF="#wxclassinfooverview">wxClassInfo</A><BR>
<A HREF="#topic1144">Example</A><BR>
<P>

<HR>
<A NAME="wxclassinfooverview"></A>
<H3>wxClassInfo</H3>
<P>
<A HREF="wx304.htm#runtimeclassoverview">Run time class information overview</A><P>
Class: <A HREF="wx42.htm#wxclassinfo">wxClassInfo</A><P>
This class stores meta-information about classes. An application
may use macros such as DECLARE_DYNAMIC_CLASS and IMPLEMENT_DYNAMIC_CLASS
to record run-time information about a class, including:<P>
<UL>

<LI> its position in the inheritance hierarchy;
<LI> the base class name(s) (up to two base classes are permitted);
<LI> a string representation of the class name;
<LI> a function that can be called to construct an instance of this class.
</UL>
<P>
The DECLARE_... macros declare a static wxClassInfo variable in a class, which is initialized
by macros of the form IMPLEMENT_... in the implementation C++ file. Classes whose instances may be
constructed dynamically are given a global constructor function which returns a new object.<P>
You can get the wxClassInfo for a class by using the CLASSINFO macro, e.g. CLASSINFO(wxFrame).
You can get the wxClassInfo for an object using wxObject::GetClassInfo.<P>
See also <A HREF="wx158.htm#wxobject">wxObject</A> and <A HREF="wx271.htm#wxcreatedynamicobject">wxCreateDynamicObject</A>.<P>

<HR>
<A NAME="topic1144"></A>
<H3>Example</H3>
<P>
In a header file wx_frame.h:<P>
<PRE>
class wxFrame: public wxWindow
{
  DECLARE_DYNAMIC_CLASS(wxFrame)

 private:
  char *frameTitle;
 public:
  ...
};
</PRE>
In a C++ file wx_frame.cc:<P>
<PRE>
IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxWindow)

wxFrame::wxFrame(void)
{
...
}
</PRE>


</BODY></HTML>