inheritance - c++ typecast base ptr to derived ptr held in a reference class -
i'm trying create object reference template class hold class pointers , works except when trying type cast base class ptr derived class ptr.
here code:
#include <stdio.h> #include <stdlib.h> #define null nullptr class basetype; class derivedtype; template<class t> class objref { public: t *ptr = null; //should private objref& operator= (t *ptr) { this->ptr = ptr; return *this; } objref& operator= (const objref &ref) { ptr = ref.ptr; return *this; } operator t*() const {return ptr;} operator t() const {return *ptr;} objref() {} objref(const objref ©) { ptr = copy.ptr; } #ifdef void_fix objref(void*p) { ptr = (t*)p; } //this fix bug except not work multiple inheritance , not safe #else objref(t*p) { ptr = p; } #endif ~objref() { } t* operator->() const {return ptr;} //unfortunately operation. (dot) can not overloaded - make life easy :( }; class object {}; class basetype : public object { public: int basevalue; }; class derivedtype : public basetype { public: operator basetype*() {return (basetype*)this;} //helpful? int derivedvalue; }; typedef objref<basetype> base; typedef objref<derivedtype> derived; void func4(base x) { x->basevalue = 1; } void func5(derived x) { x->derivedvalue = 1; } int main() { base b = null; derived d = null; base x; x = d; //no type cast needed func4((base)d); //cast derived base class - no problem b = d; //base point derived // func5((derived)b); //cast base derived - not work (desired syntax) // gcc -fpermissive can used change error warning - can silence warning ? // cl.exe equivalent be? // func5((derived)b.ptr); //invalid cast, ptr should private // func5((derivedtype*)b); //invalid cast, ptr should private // func5(dynamic_cast<derivedtype*>(b.ptr)); //invalid cast, source type not polymorphic, ptr should private func5((derivedtype*)b.ptr); //this works undesired , ptr should private func5(static_cast<derivedtype*>(b.ptr)); //works again undesired , ptr should private return 0; }
in example objref<> template holds pointer class defined , used new type. works fine except trying cast base class derived class. @ how func5() being called base class reference. first attempt desired syntax. if use ptr inside reference class work undesired.
i feel i'm missing operator or something.
thanks.
as usual found solution moments after posting.
objref<> class needs know base class. here new code:
#include <stdio.h> #include <stdlib.h> #define null nullptr class basetype; class derivedtype; template<class t> class objref { public: t *ptr = null; objref& operator= (auto *ptr) { this->ptr = ptr; return *this; } objref& operator= (const objref<auto> &ref) { ptr = ref.ptr; return *this; } operator t*() const {return ptr;} objref() {} objref(const objref<auto> ©) { ptr = (t*)copy.ptr; } objref(t*p) { ptr = p; } ~objref() { } t* operator->() const {return ptr;} //unfortunately operator. (dot) can not overloaded - make life easy :( }; class object {}; class basetype : public virtual object { public: int basevalue; }; class interfacetype : public virtual object { public: virtual void func7() { printf("it works!\n"); }; }; typedef objref<interfacetype> interface; class derivedtype : public basetype, public interfacetype { public: int derivedvalue; }; typedef objref<basetype> base; typedef objref<derivedtype> derived; void func4(base x) { x->basevalue = 1; } void func5(derived x) { x->derivedvalue = 2; } void func6(interface x) { x->func7(); } int main() { base b = new basetype(); derived d = new derivedtype(); base x; x = d; //no type cast needed func4((base)d); //cast derived base class - no problem printf("basevalue=%d\n", d->basevalue); b = d; //base point derived func5((derived)b); //cast base derived - works now! printf("derivedvalue=%d\n", d->derivedvalue); func6(d); return 0; }
update : posted new version work level of inheritance requires c++14 (auto keyword)
update #2 : uses single objref class (still requires c++14). unfortunately cl.exe doesn't support 'auto' template parameters yet :(
update #3 : added test code prove works.
Comments
Post a Comment