通过指针调用C++中私有虚函数

来源:互联网 发布:淘宝上的止鼾器有用吗 编辑:程序博客网 时间:2024/06/01 08:50

本文引用该链接博客
http://blog.csdn.net/zhoudaxia/article/details/5473784#reply
通过指针实现了调用C++类里面的私有虚函数。没有注释,请移步至上述链接看注释

#include <stdio.h>#include <stdint.h>#include <qdebug.h>class Coo{private:    int b;    virtual void fun_c();    virtual void fun_d();    virtual void fun_e();public:    Coo(int b = 27):b(b){}};void Coo::fun_c(){    printf("hello world in c\n");}void Coo::fun_d(){    printf("hello world in d\n");}void Coo::fun_e(){    printf("hello world in e\n");}int get_addr_c(){    Coo p;    return *(int*)(*((int*)(&p)));}int get_addr_d(){    Coo p;    return *(int*)(*((int*)(&p))+4);}int get_addr_e(){    Coo p;    return *(int*)(*((int*)(&p)) + 8);}int main(void){    typedef void (*P)();    get_addr_d();    P pc = (P)get_addr_c();    pc();    P pd = (P)get_addr_d();    pd();    P pe = (P)get_addr_e();    pe();    return 0;}
原创粉丝点击