i have design question. want custom datatypes implementing interface. example, using templates (maybe next design isn't correct -because can generic class instead of next- clarifies goal):
template <typename t> class idatatype { public: virtual t getdata() const = 0; virtual void setdata(t pdata) = 0; }; class mychar: public idatatype<char> { public: void setdata(char pdata){...} char getdata() const{...} private: char _data; }; class myint: public idatatype<int> { public: void setdata(int pdata){...} int getdata() const{...} private: int _data; }; idatatype<int> *data = new myint(); // parametrized interface, bad idea :( data->getdata(); // works ok
from previous classes, easy attribute corresponding each _data class member. question:
is there way (change design, etc.) implement generic setter , getter in idatatype , type , manipulate _data attribute of each class without using templates in interface?
for example:
class idatatype { public: // pure virtual getters , setters specialized _data fields. here design question. }; class mychar: public idatatype { public: void setdata(char pdata){...}; char getdata(){...}; private: char _data; }; class myint: public idatatype { public: void setdata(int pdata){...}; int getdata(){...}; private: int _data; }; idatatype *intdata = new myint(); // no parametrized interface! intdata->getdata(); // how can create method idatatype? idatatype *chardata = new mychar(); chardata->getdata(); // same here
note: have no english, apologize errors :)
you achieve in 3 ways, none elegant , error free using template
- define data union of int/float/char in base class , act on union set/get methods of base class. entire vb (old vb 6) class system works on such data type called variant.
- return void * base class , cast , use appropriate - yuck & luck!!.
- return base interface reference getdata though appearing meaningful, has no meaning @ all. 4.
Comments
Post a Comment