Я пытаюсь получить адрес функции-члена, но не знаю как. Буду признателен, если кто-нибудь скажет мне, что я делаю не так. Как вы можете видеть в моем примере ниже, ни (long) & g, ни (long) & this-> g не работают, и я не могу понять правильный синтаксис:
/* 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();
}
Заранее спасибо!
Спасибо за ваш ответ! Однако я все еще не заставляю его работать. Если я изменю строку
PR( (long)&g );
до
PR( (long)&test::g );
, это все еще не работает.
PR( &test::g );
работает в main (), но не
PR( (long)&test::g );
???
Я думаю, что что-то упустил. (