c++call back

#include "stdafx.h"
struct A;

typedef void(A::*MemFuncPtr) (int* e);

class A
{
    int a;
}; 

class View : public A
{
public:
    virtual void test() { printf("test A\n"); }
};

class Window : public View
{
public:
    virtual void test() { printf("test A\n"); }
    void func(int * b) { (void)b; printf("************\n"); }
};

typedef void(*HandlerNoParam)();
typedef void(*Handler1Param)(void* receiver);
typedef void(*Handler2Param)(void* receiver, int* e);

void test1()
{
    printf("no parameter test:\n");
}

void test2(void* receiver)
{
    (void)receiver;
    printf("1 parameter test:\n");
}

void test3(void* receiver, int* e)
{
    (void)receiver;
    (void)e;
    printf("2 parameter test:\n");
}

int _tmain(int argc, _TCHAR* argv[])
{
    Handler2Param p0 = (Handler2Param)test2;
    p0(NULL, 0);

    MemFuncPtr p = (MemFuncPtr)&Window::func;
    Window a;
    (a.*p)(NULL);
    getchar();
    return 0;
}
时间: 2024-12-28 14:56:30