![]() | Programming Guide | WideStudio Index Table of contents |
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 chapter.
- Casting of the casted pointer with the method of WSCbase: cast()
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.
The method of casting Description void* WSCbase::cast(char* className) Returns the pointer of the specified class.
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.
Copyright(C) T. Hirabayashi, 2000-2002 | Last modified: Oct 27, 2002 |