![]() |
How to cast the WSCbase* into the specified classTo 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.
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 3.0 For Use with Wide Studio Release 3.0, Summer 2002
|