Created
November 6, 2011 02:30
-
-
Save tilgovi/1342375 to your computer and use it in GitHub Desktop.
Function building with templates and overloading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
template <typename Ret, typename Arg> | |
class Prod { | |
private: | |
Ret (*left)(Arg); | |
Ret (*right)(Arg); | |
public: | |
Prod(Ret (*l)(Arg), Ret (*r)(Arg)) { | |
this->left = l; | |
this->right = r; | |
} | |
Ret operator()(Arg a) { | |
return this->left(a)*this->right(a); | |
} | |
}; | |
double div3(double a) { | |
return a/3.0; | |
} | |
double div2(double a) { | |
return a/2.0; | |
} | |
int main(int argc, char *argv[]) { | |
Prod<double, double> p = Prod<double, double>(div3, div2); | |
std::cout << p(9) << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment