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