类函数第一个参数this,我却没法取出它的地址?
我知道C++把类的成员函数作为类函数的第一个参数,隐藏了。
但是我想通过强制转型把它取出来,发现不行。
例如下面这个代码:
- C/C++ code
struct s{ int i; s():i(123){} void f(int k){// 既然k是第二个参数,this是第一个参数,我期望强转k的地址基础上减去1个指针大小,就是this s** pps=(s**)&k; // 我把函数的参数列表看成是s*的数组 s* ps =pps[-1]; cout<<"getaddress"<<endl; cout<<ps->i<<endl; cout<<this-pps[-1]<<endl;//检验是不是this指针参数紧贴着函数的第二个参数,预期的结果是sizeof(size_t) }}o1;int main(void){ o1.f(22);}VC2010在32位xp上面打印了getaddress以后就崩溃了。GCc也是core dump。
为什么呢?
[解决办法]
this是 s*. 但是, 不是左值。
s*p=this;
s**v=&p;
v[-1]?
[解决办法]
this指针有可能是通过ECX传递的
[解决办法]
- C/C++ code
#include<iostream>using namespace std;struct s{ int i; s() : i(123){} void __stdcall f(int k)const { cout<<this<<endl; cout<<*(const s *const*)(&k - 1)<<endl; }}o1;int main(void){ o1.f(22); system("pause"); return 0;}
[解决办法]
http://blog.csdn.net/mougaidong/article/details/6894563
[解决办法]
[解决办法]
windows mingw+gcc,无报错,支持2楼
[解决办法]