0

I have a class Foo.

class Foo
{
public:
    int(_bar)(const int);

    Foo(int(bar)(const int))
    {
        _bar = bar;
    }
};

I am trying to pass in a pointer to a static function on creation, and retain that in the class so I can call it later.

I am getting an error of...

error C2659: '=' : function as left operand

...but I don't understand why.

Can anyone please advise?

1 Answer 1

2

Function:

int(_bar)(const int);

Function pointer:

int(*_bar)(const int);

You just forgot the *

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. But why is this not needed on the param?
It is needed if you want to take a function pointer as a parameter, the error just happens to occur on the member because you are trying to assign to it, which you can do with a function pointer but not with a function (if that even makes sense).
So is Foo(int(bar)(const int)) invalid as a constructor definition if I were to pass the method like so: Foo(&someStaticFunc);
...because it does compile?
Well interestingly enough, in a parameter declaration contexte it seems you can assign a parameter of type function when you call the function taking that parameter, it does probably act as a function pointer, but in other contexts (within a class, function, or the global scope) that does not work.

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.