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