contents up previous next

runtime class information (aka rtti) overview

classes: wxobject, wxclassinfo.

one of the failings of c++ used to be that no run-time information was provided about a class and its position in the inheritance hierarchy. another, which still persists, 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.

most c++ gui frameworks overcome these limitations by means of a set of macros and functions and wxwidgets is no exception. as it originated before the addition of rtti to the c++ standard and as support for it is still missing from some (albeit old) compilers, wxwidgets doesn't (yet) use it, but provides its own macro-based rtti system.

in the future, the standard c++ rtti will be used though and you're encouraged to use whenever possible the wxdynamiccast() macro which, for the implementations that support it, is defined just as dynamic_cast<> and uses wxwidgets rtti for all the others. this macro is limited to wxwidgets classes only and only works with pointers (unlike the real dynamic_cast<> which also accepts references).

each class that you wish to be known to 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.

variations on these macros are used for multiple inheritance, and abstract classes that cannot be instantiated dynamically or otherwise.

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.

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.

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.

the function wxcreatedynamicobject 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.

wxclassinfo
example


wxclassinfo

runtime class information (aka rtti) overview

class: wxclassinfo

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:

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.

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.

see also wxobject and wxcreatedynamicobject.


example

in a header file frame.h:

class wxframe : public wxwindow
{
declare_dynamic_class(wxframe)

private:
    wxstring m_title;

public:
    ...
};
in a c++ file frame.cpp:

implement_dynamic_class(wxframe, wxwindow)

wxframe::wxframe()
{
...
}