Demo entry 6362245

aaa

   

Submitted by anonymous on May 08, 2017 at 05:51
Language: C++. Code size: 474 Bytes.

#include<bits/stdc++.h>  
using namespace std;  
typedef void(*F)();  
  
class A{  
public:  
    F** vptr;//指向虚函数表的指针  
    int x=1;  
    A():vptr((F**)this){}  
    virtual void f0(){puts("f0");}  
    virtual void f1(){printf("f1=%d\n",x);}  
};  
  
int main(){  
    A a;  
    a.f0();  
    a.f1();  
  
    F* vtbl=*a.vptr;//虚函数表,是个数组,数组每个元素是一个函数指针  
    vtbl[0]();//(*a.vptr)[0]();  
    vtbl[1]();//(*a.vptr)[1]();输出的x为不确定值,因为没有传入this吗?  
}  

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).