现在的位置: 首页 > 综合 > 正文

深入理解成员函数指针

2013年04月02日 ⁄ 综合 ⁄ 共 696字 ⁄ 字号 评论关闭

转自:

http://blog.csdn.net/hairetz/article/details/4153252

#include<iostream>
#include<stdio.h>
using namespace std;

class test
{
public:
test(int i){ m_i=i;}
test(){};
void hello()
{
printf("hello\n");
}
private:
int m_i;
};

int main()
{
 test *p=new test();
 p->hello();
 p=NULL;
 p->hello();
}
/****
hello
hello 

为何

p=NULL;  p->hello();这样之后,NULL->hello()也依然有效呢?

****/

        

#include<iostream>
#include<stdio.h>
using namespace std;

class test
{
public:
test(int i){ m_i=i;}
test(){}
void hello()
{
printf("hello\n");
}
private:
int m_i;
};
typedef void (test::*HELLO_FUNC)();

int main()
{
 test *p=new test();
 test q;
 p->hello();
 HELLO_FUNC phello_fun=&test::hello;
 printf("%p\n",phello_fun);
 p=NULL;
 //phello_fun=&test::hello;
 printf("%p\n",phello_fun);
 /**
hello
0041C9CC
0041C9CC
 **/
}

抱歉!评论已关闭.