c++ - function pointer point to a member function

July 07, 2022 | Admin |

c++ - function pointer point to a member function

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.

Related Posts

ServSafe® - Manager

All purchases of online products, including but not limited to online courses, online exams, and Pearson Vue Test Center products are final, nonrefundable, and nonreturnable. Online courses expire after one year from the date of purchase and 90 days from date of launch. The Pearson Vue Test Center option expires one year from date of purchase.

This website uses cookies to offer you a better browsing experience. By continuing to the website, you consent to our use of cookies. To learn more please read our updated Privacy Policy.

ServSafe is administered by the National Restaurant Association. We understand the importance of our program in teaching and certifying responsible food service measures.

We stay on top of the various and changing regulatory requirements for every state and ensure that our certification...

Read more >>

OSHA’s Required Training Frequencies - HSI

OSHA REGULATION: 1910.95; 1910.145. Employers with working environments where noise exceeds OSHA’s permissible exposure limit (PEL) of 90 A-weighted decibels (dBA) over an 8-hour workday, need to provide training on the impact of noise on the human ear, and how employees can and must protect themselves.

By Jill James | Workplace Safety

EMERGENCY RESPONSE

OSHA REQUIRED: YES

OSHA REGULATION: 1917; 1910.35 – 1019.38; 1926.159

This is training all employers must be conducting. Employers have a responsibility to provide workforce training on appropriate response to certain types of emergencies that could affect working environments, including actions employees should take to protect themselves. Such emergencies include fires, unexpected chemical...

Read more >>

Boost Your Hospitality Career with Online Classes

CTI currently offers online hospitality classes, which can help ease scheduling conflicts for hospitality workers who currently work full-time. CTI’s programs offer a basic understanding of the hospitality field and explore specific topics within the discipline. Students will receive hands-on training for key skills needed in day-to-day ...

CALL TODAY!   888-516-5339

CALL TODAY! 888-516-5339

By clicking above, I consent to receive telephone calls and text messages from CTI containing information about their programs to the number above. I understand that the calls and messages may be initiated with automated equipment and that I am not required to provide this consent to be eligible to enroll.

Hospitality is a very broad industry that involves lodging, food and beverage, entertainment, and travel. It...

Read more >>