Pointer to method is a technique not widely used (I think), and so few people know. I had to do some research to find out that you need to use additional parentheses when using the pointer. Note that it is necessary to have an object to call the method through the pointer.
This applies to nonstatic methods. Pointers for static methods are not special, they are declared in the same way as pointers for "common" functions (which are not members of a class).
#include <iostream>
using namespace std;
class Menu
{
public:
int validaEscolha();
static int stValidaEscolha();
int inicial();
};
int Menu::validaEscolha()
{
cout << 1232 << endl;
return 1;
}
int Menu::stValidaEscolha()
{
cout << 1232 << endl;
return 1;
}
int Menu::inicial()
{
// ponteiro para método não estático
int (Menu::*funcs)() = &Menu::validaEscolha;
(this->*funcs)();
// ponteiro para método estático
int (*stFuncs)() = &Menu::stValidaEscolha;
stFuncs();
return 0;
}
int main()
{
Menu m;
m.inicial();
}
In C++ it is usually used
functors
or better yet Amble in newer versions (already 6 years). Pointer to function is C thing. And there are several errors there.– Maniero
Can you give me an example of what this would look like using what you said?
– Rafael Bernardo
D take a look at this: http://en.cppreference.com/w/cpp/language/lambda
– Maniero