How to create user defined type template class object C++ -



How to create user defined type template class object C++ -

i struggling allowing user select info type template created as. since template type must defined on compile, must specify info type template utilize eg(string,int, on), means cannot alter latter on, lets string int if template supports it, because template class object declared string. class declaration below:

template <class t> class myhashtable { public: string deleted="deleted"; unsigned short tablesize; // array of vectors, hash table container vector<t>* mytable; vector<t>* deletiontable; myhashtable(unsigned short tablesize) : mytable(new vector<t>[tablesize]), deletiontable(new vector<t>[tablesize]) { this->tablesize=tablesize; }

object declaration outside class

myhashtable <string>* mychainedtable=null ; string tabletype;

object initialization

if (mychainedtable) { delete mychainedtable; mychainedtable=null; } gettype(); if (!mychainedtable) { if (tabletype=="string") mychainedtable= new myhashtable<string>(length); if (tabletype=="char") myhashtable<char> mychainedtable(length); // no difference or without using new keyword if (tabletype=="double") myhashtable<double> mychainedtable(length); if (tabletype=="float") myhashtable<float> mychainedtable(length); if (tabletype=="int") myhashtable<int> mychainedtable(length); cout<<tabletype<<" table of size "<< length<<" created"<<endl;

i attempted passing class object functions instead of having global variable, couldnt work either.

what need single template object can have: int,string,char,double,float types, have 3 functions need have access template class object, , having 5 different objects , 200 lines of if statements each situation sounds worst possible solution. been stuck on while , cant figure out how , help appreciated.

void gettype() { cout<<"enter table type, types available: int, char, float, double, string.\n"; tabletype=getinput(); while((tabletype != "int")&&(tabletype !="float")&&(tabletype !="double")&&(tabletype!="char")&&(tabletype !="string")) { cout<<"invalid type, please seek 1 time again "<<endl;; tabletype=getinput(); } }

your question @ boarder between templates , variants.

the template compile time. have take @ compile time type want object. conditional approach can't work (see comments question).

on other side, seem need dynamic selection of type @ runtime.

if want go on on template way: (edit based on comments)

you'd need have templates inherit single polymorphic base of operations class (one mutual interface virtual functions). example:

class myhashbase // mutual base of operations class templates { public: virtual void addelement(void *ptrelem) = 0; // adding element must implemented template. void* since future template type unknown base of operations class virtual void displayall() = 0; };

the templates need implement virtual functions:

template <class t> class myhashtable : public myhashbase { public: unsigned short tablesize; vector<t>* mytable; // leave is, implement these vector<t> instead of vector<t>* vector<t>* deletiontable; myhashtable(unsigned short tablesize) : mytable(new vector<t>[tablesize]), deletiontable(new vector<t>[tablesize]), tablesize(tablesize) { } void addelement(void* ptrelem) { mytable->push_back(*reinterpret_cast<t*>(ptrelem)); } // reinterpret void* of mutual interface t* void displayall() { copy(mytable->begin(), mytable->end(), ostream_iterator<t>(cout, "\n")); } };

you have mychainedtable pointer mutual base of operations type, , intialise pointer in way did string case (i.e. using new).

myhashbase *mychainedtable = nullptr; //... if (tabletype == "string") mychainedtable = new myhashtable<string>(length); else if (tabletype == "double") mychainedtable = new myhashtable<double>(length); //...

you utilize mutual api, illustration if tabletype "double":

double d1 = 3.1415, d2 = 1.4142; mychainedtable->addelement(&d1); // attention: must ensure provide pointer right info type mychainedtable->addelement(&d2); mychainedtable->displayall();

you'll have coupe of if required in calling code, cut down them minimum designing base of operations class (for example, add together virtual clone function, duplicate info without need know type caller).

however, using single signature mutual functions of base of operations class cumbersome. create virtualisation possible need pass parameters through void* pointer not nice , rather error prone.

alternate way variants

you utilize boost variants meant managing objects dynamic definition of types.

in case not need template own info structure. create myhashtable elements of type boost::variant< int, std::string, ... >.

you access right value of object if know type (as in mychainedtable) using: boost::get<int> (element) (or boost::get<string>(), ...).

if don't know type on element utilize concept of "visitor" chose automatically appropriate function exectue depending on type.

edit: alternate way unions:

if you're not allowed utilize variants alternative utilize union. don't know topic of rassignment, have selection whether utilize union define elements (like variants, without templates) or utilize template type did, define mychainedtable union of pointers different template instantiations. yes, requires lot of ifs...

c++ templates

Comments

Popular posts from this blog

php - Edges appear in image after resizing -

ios8 - iOS custom keyboard - preserve state between appearances -

Delphi change the assembly code of a running process -