看代码说话!
# include <bits/stdc++.h>
using namespace std;
//typedef void(*Fun)(void) //(C风格)
using Fun = void(*)(void); //(C++11风格)
class Base{
public:
virtual void func1(){ cout << "virtual func1 is called " << endl; }
virtual void func2(){ cout << "virtual func2 is called " << endl; }
virtual void func3(){ cout << "virtual func3 is called " << endl; }
};
int main(){
Base b;
cout << "vfptr指针(内容指向vftable地址):" << (int *)(&b) << endl;
Fun pFun = (Fun)*((int*)*(int*)(&b)+0); pFun();
pFun = (Fun)*((int*)*(int*)(&b)+1); pFun();
// 可调用对象包装器
function<void()> f = (Fun)*(((int*)*(int*)(&b)+2)); f();
cout << endl;
int a = 10;
int *ptr = &a;
int **ptr2 = &ptr;
cout << "&a = " << &a << endl;
cout << "ptr = "<< ptr << endl;
cout << "&ptr = "<< &ptr << endl;
cout << "*ptr = "<< *ptr << endl;
cout << endl;
cout << "ptr2 = " << ptr2 << endl;
cout << "&ptr2 = "<< &ptr2 << endl;
cout << "*ptr2 = "<< *ptr2 << endl;
cout << endl;
cout << "(int *)(ptr2) = " << (int *)(ptr2) << endl;
cout << "*(int *)(ptr2) = " << *(int *)(ptr2) << endl;
cout << "(int *)*(int *)(ptr2) = " << (int *)*(int *)(ptr2) << endl;
cout << "*(int *)*(int *)(ptr2) = " << *(int *)*(int *)(ptr2) << endl;
return 0;
}
输出如下:
vfptr指针(内容指向vftable地址):0x6afee8
virtual func1 is called
virtual func2 is called
virtual func3 is called
&a = 0x6afee4
ptr = 0x6afee4
&ptr = 0x6afee0
*ptr = 10
ptr2 = 0x6afee0
&ptr2 = 0x6afedc
*ptr2 = 0x6afee4
(int *)(ptr2) = 0x6afee0
*(int *)(ptr2) = 7012068
(int *)*(int *)(ptr2) = 0x6afee4
*(int *)*(int *)(ptr2) = 10
可以看出来,类似(int *)*(&b)这样的操作,主要是通过二级指针访问一级指针存储内容 ,上示代码中关于ptr和ptr2的相关操作解释:
(int *)(ptr2)
是指二级指针中存放的一级指针的地址;*
是取出 一级指针的地址 上的值,也就是一级指针本身存储的值;(int *)
是将从一级指针取到内容指针化,因为是一级指针存储的还是一块地址,否则直接按cout输出会输出整型 。