c++ - Combining multiple functions to single generic function -


i have couple of function initialise interface pointer , each function initialise pointer particular version. have make these functions single generic function.

bool init_9(mstsclib::imsrdpclient9* iface) {     iface->putsomedata1();     iface->putsomedata2();     iface->putsomedata3(); }   bool init_8(mstsclib::imsrdpclient8* iface) {     iface->putsomedata2(); }  bool init_7(mstsclib::imsrdpclient7* iface) {     iface->putsomedata1();     iface->putsomedata3(); } 

i want know if there better implementation below prototype because each statement require explicit casting of interface pointer , visual studio intellisense have hard time fetching details.

bool init(void* ptriface, int version) {     void* iface; // todo: make type required version      // cast iface @ run-time according version number     // switch(version){}     iface = reinterpret_cast<mstsclib::imsrdpclient9*>(ptriface);     iface = reinterpret_cast<mstsclib::imsrdpclient8*>(ptriface);     iface = reinterpret_cast<mstsclib::imsrdpclient7*>(ptriface);      // switch(version){}     iface->putsomedata1();     iface->putsomedata2();     iface->putsomedata3();     iface->putsomedata4();     } 

seems you're looking function overloading:

bool init(mstsclib::imsrdpclient9*); bool init(mstsclib::imsrdpclient8*); bool init(mstsclib::imsrdpclient7*);  bool init(mstsclib::imsrdpclient9* iface) {     iface->putsomedata1();     iface->putsomedata2();     iface->putsomedata3(); }   // ... 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -