- Table of Contents
- Copyright
- About the Authors
- About the Contributors
- Acknowledgments
- Tell Us What You Think!
- Introduction
- How to Use This Book
- What You Need to Use This Book
- What's New in Visual C++ 6.0
- Contacting the Main Author
- Part I: Introduction
- Chapter 1. The Visual C++ 6.0 Environment
- Part II: MFC Programming
- Chapter 2. MFC Class Library Overview
- Chapter 3. MFC Message Handling Mechanism
- Chapter 4. The Document View Architecture
- Chapter 5. Creating and Using Dialog Boxes
- Chapter 6. Working with Device Contexts and GDI Objects
- Chapter 7. Creating and Using Property Sheets
- Chapter 8. Working with the File System
- Chapter 9. Using Serialization with File and Archive Objects
- Part III: Internet Programming with MFC
- Chapter 10. MFC and the Internet Server API (ISAPI)
- Chapter 11. The WinInet API
- Chapter 12. MFC HTML Support
- Part IV: Advanced Programming Topics
- Chapter 13. Using the Standard C++ Library
- Chapter 14. Error Detection and Exception Handling Techniques
- Chapter 15. Debugging and Profiling Strategies
- Chapter 16. Multithreading
- Chapter 17. Using Scripting and Other Tools to Automate the Visual C++ IDE
- Part V: Database Programming
- Chapter 18. Creating Custom AppWizards
- Chapter 19. Database Overview
- Chapter 20. ODBC Programming
- Chapter 21. MFC Database Classes
- Chapter 22. Using OLE DB
- Chapter 23. Programming with ADO
- Part VI: MFC Support for COM and ActiveX
- Chapter 24. Overview of COM and Active Technologies
- Chapter 25. Active Documents
- Chapter 26. Active Containers
- Chapter 27. Active Servers
- Chapter 28. ActiveX Controls
- Part VII: Using the Active Template Library
- Chapter 29. ATL Architecture
- Chapter 30. Creating COM Objects Using ATL
- Chapter 31. Creating ActiveX Controls Using ATL
- Chapter 32. Using ATL to Create MTS and COM+ Components
- Part VIII: Finishing Touches
- Chapter 33. Adding Windows Help
- Part IX: Appendix
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.
Debugging Support | Next Section

Account Sign In
View your cart