c++ - Cast one derrived class to another without changing base class -
i have several child classes having same parent. each child class can constructed using data contained in parent object. cast 1 child child using info contained in base object (without modifying base object).
currently implemented illustrated in following example:
#include <iostream> using namespace std; class data {}; class base { public: base() {} base(data input) : data(input) {} virtual ~base() { cout << "deleting :" << this->name() << endl; } template<class t> static base* casttoderrived(base* object) { t* output = new t(object->data); delete object; return output; } virtual const char* name() {return "base";} data data; }; class derrived1 : public base { public: derrived1() {} derrived1(data input): base(input){} ~derrived1(){cout << "deleting :" << this->name() << endl;} const char* name(){return "derrived1";} }; class derrived2 : public base { public: derrived2(){} derrived2(data input): base(input){} ~derrived2(){cout << "deleting :\t" << this->name() << endl;} const char* name(){return "derrived2";} }; int main(int argc, char *argv[]) { base* object = new derrived1(); cout << "created :\t"<<object->name()<<endl; object = base::casttoderrived<derrived2>(object); cout << "casted :\t"<<object->name()<<endl; } which outputs:
created : derrived1 deleting :derrived1 deleting :base casted : derrived2 however, requires base class destroyed , created again, avoid - destroy derrived1, use base construct derrived2, keep base class intact. best way that?
(there several derived classes, base class provides common interface all, derived class added later without modifying base).
you can't destroy derived class keep base class because it's same instance. can demonstrate using folowing code
#include <iostream> using namespace std; class data {}; class base { public: base() { printf("base pointer = %08x\n", this); } }; class derrived1 : public base { public: derrived1() { printf("derrived1 pointer = %08x\n", this); } }; int main(int argc, char *argv[]) { base* object = new derrived1(); delete object; return 0; } the output show base derived1 same pointer on other hand use tricky code achieve want. idea use pointer data class instead of variable. code become folowing
#include <iostream> using namespace std; class data {}; class base { public: base(){data = new data} base(data* input) : data(input) {} virtual ~base() { cout << "deleting :" << this->name() << endl; } template<class t> static base* casttoderrived(base* object) { t* output = new t(object->data); delete object; return output; } virtual const char* name() {return "base";} data* data; }; class derrived1 : public base { public: derrived1() {} derrived1(data* input): base(input){} ~derrived1(){cout << "deleting :" << this->name() << endl;} const char* name(){return "derrived1";} }; class derrived2 : public base { public: derrived2(){} derrived2(data* input): base(input){} ~derrived2(){cout << "deleting :\t" << this->name() << endl;} const char* name(){return "derrived2";} }; int main(int argc, char *argv[]) { base* object = new derrived1(); cout << "created :\t"<<object->name()<<endl; object = base::casttoderrived<derrived2>(object); cout << "casted :\t"<<object->name()<<endl; } i wish help.
Comments
Post a Comment