怎样在编译期间求类成员的地址与this的偏移值?
孩皮妞野 2006-04-22 08:38:30 大体意思如下
class C{
int a;
char b;
const static int delta=/* your code to get the value
* of (char*)&this.b-(char*)this
* at complie time
*/
};
之所以有此一问是在g++中类似下面的代码发现了我没有预料的情况
class C{
struct S{
int func(...){
// 需要从S的this求出 C的this
}
};
int v;
S s; // 类C有且仅有一个S的实例, 在S::func中
// 需要从&this->s 反求出this的值
// 本来以为这个偏移值一定是-4, 结果发现g++中
// 居然是5
};