Function Pointers in C++ Mar 29, 2018 C++, Functional, Pointers David Egan In C++ like many other languages, functions can be assigned to variables. Functions can be passed into other functions as parameters. Full Example #include <iostream> #include <vector> typedef std::vector<int> intVec; // Define std::vector type for brevity // Declare functions // --------------------------------------------------------------------------------------------------------------------- // This function receives the function pointer as third parameter intVec combineVecs(intVec vec1, intVec vec2, int(*func)(int a, int b)); // These functions will be assigned to function pointers: int multiply(int a, int b); int xorInts(int a, int b); int main(int argc, char const *argv[]) { intVec v1 = { 1, 1, 1, 0 }; intVec v2 = { 0, 1, 0, 1, 10 }; // Set up function pointers // ----------------------------------------------------------------------------------------------------------------- // Declare a function pointer variable. // Defines a symbol xorEls, pointer to a function that takes two ints as arguments and returns an int int (*xorEls)(int a, int b); // Set it with the address of a function (the use of '&' is optional). xorEls = &xorInts; // Pass the function pointer for use in another function // ----------------------------------------------------------------------------------------------------------------- // Function pointer is the third function argument. intVec xored = combineVecs(v1, v2, xorEls); for(int& val : xored) { std::cout << val << std::endl; // Output result } // Repeat with a different function to demonstrate the point of function pointers // ----------------------------------------------------------------------------------------------------------------- // Output a new vector which holds the product of each element in two input vectors. int (*multiplyEls)(int a, int b); multiplyEls = &multiply; intVec multiplied = combineVecs(v1, v2, multiplyEls); for(int& val : multiplied) { std::cout << val << std::endl; } return 0; } // Function definitions // --------------------------------------------------------------------------------------------------------------------- /** * Combine two integer vectors - operate on elements of input vectors in place * by means of a provided function, creating a new vector. Performs the operation * up to the length of the shortest provided vector. * @param vec1 std::vector<int> * @param vec2 std::vector<int> * @param func A function pointer * @return zipped A std::vector<int> object with each element operated on in-place. */ intVec combineVecs(intVec vec1, intVec vec2, int(*func)(int a, int b)) { intVec combined; typedef typename std::vector<int>::size_type const containerSize; containerSize vec1Size = vec1.size(); containerSize vec2Size = vec2.size(); containerSize zipLen = (vec1Size >= vec2Size) ? vec2Size : vec1Size; for (size_t i = 0; i < zipLen; i++) { // Append the result of the passed-in function to `combined` combined.push_back(func(vec1.at(i), vec2.at(i))); } return combined; } int multiply(int a, int b) { return a * b; } int xorInts(int a, int b) { return a ^ b; } References Function Pointers Tutorial Please enable JavaScript to view the comments powered by Disqus. comments powered by Disqus