指向成员函数指针,虚函数指针,静态成员函数指针

// PointtoMemberFunction.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <IOSTREAM>
using namespace std;

class A{
public:
    void f(){
        cout<<"f()"<<endl;
    }

    virtual void g(){
        cout<<"g()"<<endl;
    }

    static void h(){
        cout<<"h()"<<endl;
    }
};

int main(int argc, char* argv[])
{
    A  *pa = new A;

    typedef void (A::*pfuncf)(void);
    pfuncf testf = &A::f;
//    printf("%d\n",testf); 
    (pa->*testf)();

    typedef void (A::*pfuncg)(void);  //与普通成员函数一样
    pfuncg testg = &A::g;
//    printf("%d\n",testg); 
    (pa->*testg)();

    typedef void (*pfunch)(void);     //类中static看做一般函数,除了下面一行得加上个 A::,其余与一般函数一样
    pfunch testh = &A::h;
//    printf("%d\n",testh); 
    (*testh)();             //使用时,就不需要A::了,与一般函数指针相同
    
    return 0;
}

 

posted @ 2015-07-12 13:48  朽木可雕否  阅读(196)  评论(0编辑  收藏  举报