Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

Runtime Type Information

Beginning with version 4.0 of Visual C++, two versions of runtime type information are supported: the ANSI standard C++ typeid() variety and MFC's own, more powerful brand of type identification provided by the CObject class.

If you want to use the ANSI variety of runtime type information (RTTI), you can enable this in the Project Settings dialog box by selecting the Enable Run-Time Type Informa tion (RTTI) option on the C++ Language page of the C/C++ tab. This allows you to use the C++ typeid() operator to get the name of the class.

The MFC version of runtime type information provides backward compatibility with MFC apps that predate ANSI RTTI in Microsoft compilers and provides information for efficient serialization and cross-platform compatibility.

Using MFC Runtime Type Information

To use MFC's runtime type information, you can use the DECLARE_DYNAMIC macro, which takes your class name as an argument in your class declaration, and the IMPLEMENT_DYNAMIC macro, which takes your class name and its base class in your implementation. These set up the structures that MFC uses to track type information for your classes.

IsKindOf() and RUNTIME_CLASS()

If you have enabled runtime type identification in your classes, you can now verify that any pointer you get is a valid pointer to the class you expect, before your app goes off into the weeds from a faulty pointer. This is done by using the CObject::IsKindOf() function, which takes a pointer to a runtime type information structure. This structure can be provided for constant class types by the RUNTIME_CLASS macro, taking your class name as a parameter. This example demonstrates this a little better:

if(pMyPtr->IsKindOf(RUNTIME_CLASS(CEmployee)))
    DoSomething();
else
    DoError();

In this example, if pMyPtr points to an object of class CEmployee or any class derived from it, IsKindOf() returns TRUE. If IsKindOf() returns FALSE, you will probably want to report an error.

STATIC_DOWNCAST and DYNAMIC_DOWNCAST

In addition to the type information available from CObject, MFC provides macros to validate types when you want to cast a pointer to an object to a pointer to a more specific derived class. You can use STATIC_DOWNCAST to check your casting with code like this:

CWnd* pWnd;
pButton = STATIC_DOWNCAST(CButton, pWnd);

If you are running a debug build and pWnd is not really a pointer to a CButton class object, MFC will assert; otherwise, the cast is performed as normal. In a non-debug build, STATIC_DOWNCAST will always perform the cast without type checking. If you want to do type checking in release builds, you can use the DYNAMIC_DOWNCAST macro, which works the same way in debug and nondebug builds. If the type check fails, or if the pointer is NULL, DYNAMIC_DOWNCAST will return NULL. It is then up to your application to decide what to do.

ASSERT_KINDOF()

If you want to check that a pointer is of the type you want it to be, you could use a line like the following:

ASSERT(pMyPtr->IsKindOf(RUNTIME_CLASS(CEmployee)));

This may be a bit cumbersome both to type and to read, so MFC provides a shortcut—the ASSERT_KINDOF macro. The following line behaves exactly the same as the previous ASSERT—it just looks neater:

ASSERT_KINDOF(CEmployee, pMyPtr);

You can probably guess that the first argument is the desired class type and the second is the pointer in question.

Share ThisShare This

Informit Network