WideStudio/MWT Logo
WideStudio/MWT
Programming Guide
WideStudio/MWT Index
Table of contents


How to cast the WSCbase into the specified class

To access the method of subclass, it requires the pointer to be subclass. So we must convert (downcast) the pointer of WSCbase* into the subclass with some method. I will explain the acquisition of the casted pointer in this chapter.

The method of casting Description
void* WSCbase::cast(char* className) Returns the pointer of the specified class.

Usage of the method: WSCbase::cast() is as follows. In the following example, the pointer "object" contains a WSCvtoggle instance, but is a pointer of WSCbase*. and you want to access the WSCvtoggle method: getStatus() which returns the state of the toggle.

In C++ language, it is not allowed to downcast like WSCbase* to WSCvtoggle*. The method: WSCbase::cast() supports this.

#include "WSCvtoggle.h" //access WSCvtoggle class.
...

void cbop(WSCbase* object){
  //downcast WSCbase* to WSCvtoggle*.
  WSCvtoggle* tgl = (WSCvtoggle*)object->cast("WSCvtoggle");
  
  if (tgl == NULL){
    // It fails, because the pointer "object"
    //      is not a WSCvtoggle instance.
  }else{
    // it succeeds, the pointer "object" is a WSCvtoggle instance.
    // access the WSCvtoggle::getStatus()
    WSCbool status = tgl->getStatus();
  }
}

The method: WSCbase::cast() returns NULL,if the instance is not a instance of the specified class. if we use this specification well, we can examine the instance whether it is the specified class or not.

#include "WSCvbtn.h" //access WSCvbtn class.
#include "WSCvtoggle.h" //access WSCvtoggle class.
...

void cbop(WSCbase* object){
  //examine whether object is a WSCvlabel instance.
  WSCvlabel* btn = (WSCvlabel*)object->cast("WSCvlabel");

  //examine whether object is a WSCvtoggle instance.
  WSCvtoggle* toggle = (WSCvtoggle*)object->cast("WSCvtoggle");

  if (btn == NULL){
    //it is not a WSCvbtn instance.
  }else{
    //it is a WSCvbtn instance or inherits WSCvbtn class.
  }
  if (toggle == NULL){
    //it is not a WSCvtoggle instance.
  }else{
    //it is a WSCvtoggle instance or inherits WSCvtoggle class.
  }

}

If the event procedure is used by some instances of various classes, it is useful to switch the program.

Document Release 3.90 for WideStudio/MWT ver 3.90, Jul 2005


WideStudio/MWT documents index | Table of contents

Copyright(C) WideStudio/MWT Development Team, 1999-2005 Last modified: Jul 31, 2005