c – 此指针和成员函数地址
作者:互联网
我正在尝试获取成员函数的地址,但我不知道如何.如果有人能告诉我我做错了什么,我将不胜感激.正如您在下面的示例中所看到的,既没有(长)& g也没有(长)& 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 );
???
我想我错过了什么.
标签:c,function,memory-address,member 来源: https://codeday.me/bug/20191003/1846703.html