看了Inside The C++ Object Model后的一个问题:如何查看类成员函数的地址?
看了Inside The C++ Object Model后的一个问题:如何查看类成员函数的地址?
我写了一个程序来查看类成员函数的地址,但是在VC.net中得到的输出却是两个1,我想这种方法得到的是不是成员函数的偏移地址?但是不应该都是1啊。还有就是我没有办法查看一个类对象的成员函数的地址,写了这样的一句话,可是不能通过:
cout<<&a.xfunction<<endl;有什么办法可以查看?
#include<iostream>
using namespace std;
class X{
public:
void xfunction(){ cout<<"xfunction"<<endl; }
void xfunctionA(){ cout<<"xfunctionA"<<endl; }
private:
int i;
};
void main()
{
X a, b;
cout<<&X::xfunction<<endl;
cout<<&X::xfunctionA<<endl;
}
问题点数:30、回复次数:5Top
1 楼ckacka(/*小红帽*/ckacka();)回复于 2003-02-01 22:24:43 得分 5
在建立了类的事例后,就是
cout<<&X::xfunction<<endl;
这一步,单步运行到这里,
在watch中就可以看到了!Top
2 楼zhaobong(赵bong)回复于 2003-02-01 22:45:21 得分 0
那么如何查看一个类对象的成员函数的地址?Top
3 楼qhgary(Gary)回复于 2003-02-03 11:23:45 得分 2
你在前面加上(void)试试Top
4 楼liu_feng_fly(笑看风云 搏击苍穹 衔日月)回复于 2003-02-03 12:07:26 得分 2
cout<<&X::xfunction<<endl;
写成
cout<<(int)(&X::xfunction)<<endl;
这样试试Top
5 楼boxban(冻酸梨)回复于 2003-02-03 16:22:11 得分 21
#include<iostream>
using namespace std;
class X{
public:
void addr(){
void (X::*pmf)() = &X::xfunction;
printf("%p\n", pmf);
(this->*pmf)();
}
void xfunction(){ cout<<"xfunction"<<endl; }
void xfunctionA(){ cout<<"xfunctionA"<<endl; }
private:
int i;
};
void main()
{
X a, b;
a.addr();
}
Top




