OWLNext 7.0
Borland's Object Windows Library for the modern age
|
You might want to perform some operation on each of a parent window's child windows.
The iterator function ForEach() takes a pointer to a function. The function can be either a member function or a stand-alone function. The function should take a TWindow * and a void
After ForEach has called your function, you often need to be careful when dealing with the child object. Although the object is passed as a TWindow *, it is actually usually a descendant of TWindow. To make sure the child object is handled correctly, you should use the TYPESAFE_DOWNCAST macro to cast the TWindow * to a TClass *, where TClass is whatever type the child object is.
For example, suppose you want to check all the check box child windows in a parent window:
If the class you're downcasting to (in this case from a TWindow to a TCheckbox) is virtually derived from its base, you must use the TYPESAFE_DOWNCAST macro to make the assignment. In this case, TCheckbox is not virtually derived from TWindow, making the TYPESAFE_DOWNCAST macro seem superfluous in this case.
But TYPESAFE_DOWNCAST returns 0 if the cast could not be performed. This is how the TYPESAFE_DOWNCAST macro is used here, because not all of the children are necessarily of type TCheckbox. If a child of type TControlBar were encountered, the value of cb would be 0, thus assuring that you do not try to check a control bar.