Skip to content

Instantly share code, notes, and snippets.

@danilobellini
Created September 25, 2014 03:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danilobellini/58a461a2643e30ef6781 to your computer and use it in GitHub Desktop.
Save danilobellini/58a461a2643e30ef6781 to your computer and use it in GitHub Desktop.
Inline and function pointer example (C++ but that's almost C)
// 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