Wide Studio Programmer's Guide

Wide Studio Home
Up to


How to cast the WSCbase* into the specified class

To access a method of some subclass,it requires that the pointer is 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 capter.
  • Casting of the casted pointer with the method of WSCbase: cast()
    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 isntance 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 1.3

For Use with Wide Studio Release 1.3, Summer 2001


Wide Stuido Home | Up to

Copyright(C) T. Hirabayashi, 2000-2001 Last modified: August 20, 2001