OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
Operating On All Children: ForEach

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

  • argument. ForEach calls the function once for each child. The child is passed as the TWindow *. The void * defaults to 0. You can use the void * to pass any arguments you want to your function.

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:

void CheckTheBox(TWindow* win, void*)
{
if(cb)
cb->Check();
}
void TMDIFileWindow::CheckAllBoxes()
{
ForEach(CheckTheBox);
}
#define TYPESAFE_DOWNCAST(object, toClass)
Definition defs.h:269

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.

See Also