Created
September 25, 2014 03:34
-
-
Save danilobellini/58a461a2643e30ef6781 to your computer and use it in GitHub Desktop.
Inline and function pointer example (C++ but that's almost C)
This file contains hidden or 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
// Created on Thu Sep 25 00:23:24 2014 | |
// @author: Danilo de Jesus da Silva Bellini | |
// Inline and function pointer example (C++ but that's almost C) | |
// | |
// g++ inline_or_funcptr.cpp | |
// | |
#include <iostream> | |
using namespace std; | |
typedef int (*IntOpPtr)(int, int); | |
int sum(int a, int b){ return a + b; } | |
int mul(int a, int b){ return a * b; } | |
int sub(int a, int b){ return a - b; } | |
inline int apply(IntOpPtr op, int a, int b){ return op(a, b); } | |
int main(){ | |
IntOpPtr meths[] = {sum, mul, sub}; | |
int nmeths = sizeof(meths) / sizeof(IntOpPtr); | |
for(int i = 0; i < nmeths; i++) | |
cout << apply(meths[i], 5, 7) << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment