1

I've got a problem when trying to assign a function pointer to a function pointer in a struct.

I have a struct command, which holds an invocation string value, a message to acknowledge its activation, and a function to be called upon activation.

However, I am having trouble assigning the function pointer in the struct's constructor (might make the struct into a class later, not sure) below.

struct Command
{
    Command(string _code, string _message, void *_func(void))
        : code(_code), message(_message) { /* ERROR: */ func = _func; }

    string code;        // The string that invokes a console response
    string message;     // The response that is printed to acknowledge its activation
    void *func(void);   // The function that is run when the string is called
};

In the above code, marked by /* ERROR: */ I get the error, "expression must be a modifiable value". How can I fix this? I just want to pass a reference to a function to the struct.

3
  • 6
    void *func(void) is not a function pointer, it's a declaration of a function named func which takes no arguments and return a void*. void (*func)(void) is a variable named func which is a pointer to a function taking no arguments and returning nothing. Commented Oct 15, 2014 at 13:06
  • Also, in C++ you should be careful with pointers to functions, as those can only be used for non-member functions, static member function or one type of lambda expression. If you want a more generic function-type, read about std::function. Commented Oct 15, 2014 at 13:09
  • @Joachim Pileborg Ugh, you're right. Cool man, thanks for the answer! Works now :) Commented Oct 15, 2014 at 13:09

2 Answers 2

5

As mentioned by @Joachim Pileborg you don't declare a pointer to a function.

To declare a function pointer you need to add parentheses around the asterisk and identifier part, e.g.

// 'func' is a pointer to function taking parameter void and returning void.
void (*func)(void);

Since C++11 you can also declare a function pointer like this which is less terse:

std::add_pointer_t<void()> func;

std::add_pointer_t<void(int, int)> func; // Pointer to func taking 2 ints.
Sign up to request clarification or add additional context in comments.

2 Comments

After a quick messaround I don't fully understand add_pointer nor how to use std::function for my purposes. I'll have to look into that further. Until then, thanks for the quick fix :D
You can simply replace all occurences of void (*func)(void) with std::add_pointer_t<void()> func with no subsequent change in behaviour. Also remember that in C++ you don't have to declare a parameter list that takes nothing with (void), instead you can simply do ().
2

You need brackets around *func and *_func

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.