]> git.saurik.com Git - wxWidgets.git/blame - docs/doxygen/overviews/runtimeclass.h
Fix problem with most of wxGTK headers not being installed.
[wxWidgets.git] / docs / doxygen / overviews / runtimeclass.h
CommitLineData
15b6757b 1/////////////////////////////////////////////////////////////////////////////
58d0deaa 2// Name: runtimeclass.h
15b6757b
FM
3// Purpose: topic overview
4// Author: wxWidgets team
5// RCS-ID: $Id$
526954c5 6// Licence: wxWindows licence
15b6757b
FM
7/////////////////////////////////////////////////////////////////////////////
8
880efa2a 9/**
36c9828f 10
c83e60aa 11@page overview_rtti Runtime Type Information (RTTI)
36c9828f 12
9727442f
FM
13@li @ref overview_rtti_intro
14@li @ref overview_rtti_classinfo
15@li @ref overview_rtti_example
58d0deaa 16
adcb6f88 17@see
58d0deaa
BP
18
19@li wxObject
20@li wxClassInfo
21
22
23<hr>
24
25
9727442f 26@section overview_rtti_intro Introduction
58d0deaa 27
c83e60aa 28One of the failings of C++ used to be that no runtime information was provided
58d0deaa
BP
29about a class and its position in the inheritance hierarchy. Another, which
30still persists, is that instances of a class cannot be created just by knowing
31the name of a class, which makes facilities such as persistent storage hard to
32implement.
33
34Most C++ GUI frameworks overcome these limitations by means of a set of macros
35and functions and wxWidgets is no exception. As it originated before the
36addition of RTTI to the C++ standard and as support for it is still missing
37from some (albeit old) compilers, wxWidgets doesn't (yet) use it, but provides
e3778b4d 38its own macro-based RTTI system.
58d0deaa
BP
39
40In the future, the standard C++ RTTI will be used though and you're encouraged
41to use whenever possible the wxDynamicCast macro which, for the implementations
42that support it, is defined just as dynamic_cast and uses wxWidgets RTTI for
43all the others. This macro is limited to wxWidgets classes only and only works
44with pointers (unlike the real dynamic_cast which also accepts references).
45
46Each class that you wish to be known to the type system should have a macro
47such as DECLARE_DYNAMIC_CLASS just inside the class declaration. The macro
48IMPLEMENT_DYNAMIC_CLASS should be in the implementation file. Note that these
49are entirely optional; use them if you wish to check object types, or create
50instances of classes using the class name. However, it is good to get into the
51habit of adding these macros for all classes.
52
53Variations on these macros are used for multiple inheritance, and abstract
54classes that cannot be instantiated dynamically or otherwise.
55
56DECLARE_DYNAMIC_CLASS inserts a static wxClassInfo declaration into the class,
57initialized by IMPLEMENT_DYNAMIC_CLASS. When initialized, the wxClassInfo
58object inserts itself into a linked list (accessed through wxClassInfo::first
59and wxClassInfo::next pointers). The linked list is fully created by the time
60all global initialisation is done.
61
62IMPLEMENT_DYNAMIC_CLASS is a macro that not only initialises the static
63wxClassInfo member, but defines a global function capable of creating a dynamic
64object of the class in question. A pointer to this function is stored in
65wxClassInfo, and is used when an object should be created dynamically.
66
67wxObject::IsKindOf uses the linked list of wxClassInfo. It takes a wxClassInfo
68argument, so use CLASSINFO(className) to return an appropriate wxClassInfo
69pointer to use in this function.
70
71The function wxCreateDynamicObject can be used to construct a new object of a
72given type, by supplying a string name. If you have a pointer to the
73wxClassInfo object instead, then you can simply call wxClassInfo::CreateObject.
74
75
9727442f 76@section overview_rtti_classinfo wxClassInfo
58d0deaa
BP
77
78This class stores meta-information about classes. An application may use macros
c83e60aa 79such as DECLARE_DYNAMIC_CLASS and IMPLEMENT_DYNAMIC_CLASS to record runtime
58d0deaa
BP
80information about a class, including:
81
e3778b4d 82@li Its position in the inheritance hierarchy.
58d0deaa
BP
83@li The base class name(s) (up to two base classes are permitted).
84@li A string representation of the class name.
85@li A function that can be called to construct an instance of this class.
86
87The DECLARE_... macros declare a static wxClassInfo variable in a class, which
88is initialized by macros of the form IMPLEMENT_... in the implementation C++
89file. Classes whose instances may be constructed dynamically are given a global
90constructor function which returns a new object.
91
92You can get the wxClassInfo for a class by using the CLASSINFO macro, e.g.
93CLASSINFO(wxFrame). You can get the wxClassInfo for an object using
94wxObject::GetClassInfo.
95
adcb6f88 96@see
58d0deaa
BP
97
98@li wxObject
99@li wxCreateDynamicObject
100
101
9727442f 102@section overview_rtti_example Example
58d0deaa
BP
103
104In a header file frame.h:
105
106@code
107class wxFrame : public wxWindow
108{
109 DECLARE_DYNAMIC_CLASS(wxFrame)
110
111private:
112 wxString m_title;
113
114public:
115...
116};
117@endcode
118
119In a C++ file frame.cpp:
120
121@code
122IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxWindow)
123
124wxFrame::wxFrame()
125{
126...
127}
128@endcode
129
130*/
36c9828f 131