A SERVICE OF

logo

parameters. The first parameter is the data type you want to downcast to.
The second parameter is the class instance you want to downcast.
For example, the following code downcasts the TWindowsObject object
pointer Parent to a TWindow:
TMyChildWindow::MyFunc() {
.
.
.
// Parent is actually a TWindowsObject object.
((TWindow *)Parent)->AssignMenu("NewMenu");
.
.
.
}
Y u might try to convert this code like this, simply converting the TWindowo
class to a TFrameWindow class:
TMyChildWindow::MyFunc() { // error on next line
.
.
.
// Parent is actually a TWindow object.
((TFrameWindow *)Parent)->AssignMenu("NewMenu");
.
.
.
}
However in ObjectWindows 2.0, Parent’ type is a TWindow * (it was a, s
TWindowsObject
*), which is a virtual base of TFrameWindow. Attempting to
downcast this results in a compile-time error The correct way to convert.
this using the DYNAMIC_CAST macro is shown here:
TMyChildWindow::MyFunc() {
.
.
.
DYNAMIC_CAST(TFrameWindow*,Parent)->AssignMenu("NewMenu");
.
.
.
}
Here’ the syntax for the DYNAMIC_CAST macro:s
type
DYNAMIC_CAST(
type
,
object
)
where:
type is the data type to which you want to cast the object.
object is the object you want to cast.
If the conversion is successful, DYNAMIC_CAST returns object as a type
data object. If the conversion fails, the result of the DYNAMIC_CAST
macro is 0. Y u should perform error checking when using theo
DYNAMIC_CAST macro.
374
OWL P ogrammer’ Guider s