este endereço de ponteiro e função membro
Estou tentando obter o endereço de uma função de membro, mas não sei como. Eu apreciaria se alguém pudesse me dizer o que estou fazendo de errado. Como você pode ver no meu exemplo abaixo, nem (long) & g nem (long) & this-> g funcionam e não consigo descobrir a sintaxe correta:
/* Create a class that (redundantly) performs data member selection
and a member function call using the this keyword (which refers to the
address of the current object). */
#include<iostream>
using namespace std;
#define PR(STR) cout << #STR ": " << STR << endl;
class test
{
public:
int a, b;
int c[10];
void g();
};
void f()
{
cout << "calling f()" << endl;
}
void test::g()
{
this->a = 5;
PR( (long)&a );
PR( (long)&b );
PR( (long)&this->b ); // this-> is redundant
PR( (long)&this->c ); // = c[0]
PR( (long)&this->c[1] );
PR( (long)&f );
// PR( (long)&g ); // this doesn't work
// PR( (long)&this->g ); // neither does this
cout << endl;
}
int main()
{
test t;
t.g();
}
Desde já, obrigado!
Obrigado por sua resposta! No entanto, ainda não o faço funcionar. Se eu mudar de linha
PR( (long)&g );
para
PR( (long)&test::g );
, ainda não funciona.
PR( &test::g );
funciona em main (), mas não
PR( (long)&test::g );
???
Eu acho que estou perdendo alguma coisa. :(