Created
July 18, 2013 19:25
-
-
Save MiroK/6032222 to your computer and use it in GitHub Desktop.
C++ member-function function-pointer
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> | |
class Calculator | |
{ | |
public: | |
int operation(int a, int b) { return (this->*_operation)(a, b); } | |
void set_operation(int option) | |
{ | |
switch(option) | |
{ | |
case 0 : | |
_operation = &Calculator::add; | |
break; | |
case 1 : | |
_operation = &Calculator::subtract; | |
break; | |
} | |
} | |
private: | |
int add(int a, int b){ return a+b; } | |
int subtract(int a, int b){ return a-b; } | |
private: | |
int (Calculator::* _operation)(int, int); | |
}; | |
int main() | |
{ | |
Calculator casio; | |
casio.set_operation(0); | |
std::cout << "2 + 2 = " << casio.operation(2, 2) << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment