Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How do I declare an array of pointers to member functions?

0
Posted

How do I declare an array of pointers to member functions?

0

Keep your sanity with “typedef”. class Fred { public: int f(char x, float y); int g(char x, float y); int h(char x, float y); int i(char x, float y); //… }; typedef int (Fred::*FredPtr)(char x, float y); Here’s the array of pointers to member functions: FredPtr a[4] = { &Fred::f, &Fred::g, &Fred::h, &Fred::i }; To call one of the member functions on object “fred”: void userCode(Fred& fred, int methodNum, char x, float y) { //assume “methodNum” is between 0 and 3 inclusive (fred.*a[methodNum])(x, y); } You can make the call somewhat clearer using a #define: #define callMethod(object,ptrToMethod) ((object).

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123