Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
I am working on an optimization algorithm, in my algorithm, I need to use a function pointer to pass the function into the optimizer
the class definition:
In the declaration of member function run(), I did as:
And I had the compiler errer:
How can I refer a member function in another member function? Thank you very much!
When taking the address of a method pointer, you need to include the name of the class. So in this case, the appropriate syntax would be:
At the same time, a non-static method pointer is not interchangeable with a regular function pointer. So you would need to declare the third argument of amoeba as:
And then you would need to call the function as this->*funk(...). If you need to bundle the identity of the object with a pointer, then you might consider making the third argument a std::function<float(float*, int)> (and then using a lambda or std::bind). However in your case that might not be necessary.
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2022 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2022.7.6.42527
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.