C++ function that allows arbitrary class input -


right have class has method f:

class a{ public:     double f(double x){return 0.0;}; }; 

and have class b method f:

class b{ public:     double f(double x){return 0.0;}; }; 

i function myfun take in either class or class b without having overload function:

double myfun(a a){     return a.f(1.0); } double myfun(b b){     return b.f(1.0); } 

i don't want overload function because have 3 or more different classes method f(). there way this?

use simple template function:

template<typename t> double myfun(t t) {     return t.f(1.0); } 

and call like:

a a; myfun(a);  b b; myfun(b); 

Comments

Popular posts from this blog

visual studio - vb.net filter binding source by time -

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -