0

I'm trying to pass a function pointer to another function, which has a string array as arguments. so far I have the following:

void pass_function(string args[]) {    
    //so something with args.
}

void takes_a_function(void(*function)(string[])) {
    function;
}

int main()
{
    string s[] = { "hello", "World" };
    takes_a_function(pass_function(s));

    system("pause");    
    return 0;
}

the issue appears to be that that the parameter pass_function(s) is casting as a void rather than void(*function)(sting *)

I'm imagining it requires a cast but would prefer a cleaner was of doing it if possible.

3
  • pass_function(s) is of type void because you called the function. And then you tried to pass the return value. Commented Nov 21, 2018 at 16:32
  • 1
    your takes_a_function is noop Commented Nov 21, 2018 at 16:34
  • Could you describe what you are actually trying to achieve here? Commented Nov 21, 2018 at 16:34

2 Answers 2

1

would prefer a cleaner was of doing it if possible.

From here

takes_a_function(pass_function(s));
                 ^^^^^^^^^^^^^^^^^^

it looks like you want to pass a something callable(pass_function) to another function(takes_a_function), after binding the argument(string array). If so, you have better options in C++.

First use either std::vector<std::string> or std::array<std::string, 2>(if size is known) to store the strings. Secondly, the passing of callable to another function, by either of the following ways:

  1. Using a lambda and std::bind

    Make takes_a_function as a template function and then after binding with the argument pass the callable object(pass_function as a lambda function) to it.

    #include <vector>     // std::vector
    #include <functional> // std::bind
    
    template<typename Callable> 
    void takes_a_function(const Callable &function) 
    {
        function(); // direckt call
    }
    
    int main()
    {
        std::vector<std::string> s{ "hello", "World" };
        auto pass_function = [](const std::vector<std::string> &args) { /*do something*/ };
    
        takes_a_function(std::bind(pass_function, s));        
        return 0;
    }
    
  2. Using function pointer

    If the function pointer is unavoidable, you need two arguments in takes_a_function, one should be the function pointer and the other should be the array of strings.

    #include <vector>     // std::vector
    
    // convenience type
    using fPtrType = void(*)(std::vector<std::string> const&);
    
    void pass_function(const std::vector<std::string> &args) { /*do something*/ };
    
    void takes_a_function(const fPtrType &function, const std::vector<std::string> &args)
    {
        function(args); // call with args
    }
    
    int main()
    {
        std::vector<std::string> s{ "hello", "World" };
        takes_a_function(pass_function, s);
        return 0;
    }
    
Sign up to request clarification or add additional context in comments.

Comments

1

The correct syntax is:

takes_a_function(pass_function);

Alternatively:

void pass_function(std::string args[]);

void takes_a_function(void(*function)(std::string[]), std::string args[]) {
    function(args);
}

int main() {
    std::string s[] = { "hello", "World" };
    takes_a_function(pass_function, s);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.