我写了一些测试代码,找出成员函数的地址。但结果让我很困惑。代码是
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
class A
{
public:
A(char _a, char _b, char _c, char _t):a(_a), b(_b), c(_c), t(_t){}
private:
char t;
char a;
public:
char b;
char c;
void test1(){std::cout << a << std::endl;}
void test2(){std::cout << b << std::endl;}
void test3(){std::cout << c << std::endl;}
void test4(){std::cout << t << std::endl;}
static void print(){
char A::*pa = &A::a;
char A::*pb = &A::b;
char A::*pc = &A::c;
char A::*pt = &A::t;
printf("data member : %p, %p, %p, %p\n", pa, pb, pc, pt);
void (A::*pf1)() = &A::test3;
void (A::*pf2)() = &A::test2;
void (A::*pf3)() = &A::test3;
void (A::*pf4)() = &A::test2;
printf("function member : %p, %p, %p, %p\n", pf1, pf2, pf3, pf4 );
}
};
int main()
{
A::print();
//system("pause");//need on Dev-c++
return 0;
}
我用g++在ubuntu上运行这段代码,用Dev-C++在Windos上运行,结果是一样的
data member : 0x1, 0x2, 0x3, (nil)
function member : 0x8048780, (nil), 0x804874e, (nil)
为什么 pf2 和 pf4 为零?