#include "stdafx.h"
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <cassert>
using namespace std;

class Base
{
    int a;
public:
    virtual void fun1() {cout<<"Base::fun1()"<<endl;}
    virtual void fun2() {cout<<"Base::fun2()"<<endl;}
    virtual void fun3() {cout<<"Base::fun3()"<<endl;}
};
class A:public Base
{
    int a;
public:
    void fun1() {cout<<"A::fun1()"<<endl;}
    void fun2() {cout<<"A::fun2()"<<endl;}
};
void foo (Base& obj)
{
    obj.fun1();
    obj.fun2();
    obj.fun3();
}
void *getp (void* p)
{
    return (void*)*(unsigned long*)p;
}
typedef void (*fun)();

fun getfun (Base* obj, unsigned long off)
{
    void *vptr = getp(obj);
    unsigned char *p = (unsigned char *)vptr;
    p += sizeof(void*) * off;
    return (fun)getp(p);
}



int main(int argc, char* argv[])  
{  
    //Base b;
    //A a;
    //foo(b);
    //foo(a);

    Base *p = new A;
    fun f = getfun(p, 0);
    (*f)();
    f = getfun(p, 1);
    (*f)();
    f = getfun(p, 2);
    (*f)();
    delete p;


    return 0;
}  

相关文章:

  • 2021-07-06
  • 2021-05-02
  • 2021-08-29
  • 2021-06-26
  • 2021-08-06
  • 2021-12-24
  • 2022-12-23
  • 2021-07-16
猜你喜欢
  • 2021-12-23
  • 2022-01-18
  • 2022-12-23
  • 2022-02-20
  • 2021-11-24
  • 2022-02-02
  • 2022-12-23
相关资源
相似解决方案