Question

Is it possible to pass the name of a function(say A) as an argument to another function (say B), and then call function A from function B. That is the function name will be stored in a variable in B, and using it call the function whose name is in the variable. For Example in C++ sort function the first and second arguments are iterators, but the third argument is the name of a function.

Was it helpful?

Solution

You can introduce a template parameter (here Pred) for "anything that is callable with two parameters":

template <typename Iter, typename Pred>
void mysort(Iter begin, Iter end, Pred predicate)
{
    --end;
    // ...
        if (predicate(*begin, *end))
        {
            // ...
        }
    // ...
}

Then you can pass either good old C function pointers or C++ function objects:

bool function(int x, int y)
{
    return x < y;
}

struct function_object
{
    bool operator()(int x, int y)
    {
        return x < y;
    }
};

int main()
{
    int numbers[] = {543, 6542654, 432514, 54, 45, 243};
    mysort(numbers + 0, numbers + 6, &function);
    mysort(numbers + 0, numbers + 6, function_object());
}

As you can see, a function object is an object of a class that overloads operator() appropriately.

OTHER TIPS

You should read up on Function Pointers.

Yes, not only is it possible, but has great usage as well.

Technically, these are known as function pointers.

Conceptually, they are required to have callback mechanisms. Callbacks might be required for event-driven programs. E.g., your application might be interested to know when a mouse-button is clicked. In this case you would register your interest to the underlying platform for a mouse click event, and tell it what method/function should be called in your program, so that you can execute code accordingly.

Another such case is where programs have an asynchronous mode of execution. E.g., in case a file is to be written to hard disk, which is a time consuming process as compared to doing arithmetical calculations. So, it might be a case where we do not want to wait performing calculations while we are writing results to a file. A program might choose to just call a write function on a file and return and start calculations. While, calling this function the caller might also specify a callback function. So, when the file is written successfully or fails, the callback function is called and the callee informed accordingly.

simple example with Function Pointers:

#include <iostream>

int apply(int (*fun)(int,int), int a, int b) {
    return (*fun)(a,b);
}

int add(int a, int b) {return a + b;}
int multiply(int a, int b) {return a * b;}

int main(int argc, const char* argv[]) {
    int added = apply(add, 2, 4);
    int multiplied = apply(multiply, 2, 4);

    std::cout << "added result: " << added << std::endl;
    std::cout << "multiplied result: " << multiplied << std::endl;
}

output:

added result: 6
multiplied result: 8

Yes, it's possible. Technically, it's not quite the 'name' that's passed, but rather, actually a pointer to the function being passed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top