runtimeclass.h 4.2 KB

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