64,282
社区成员
发帖
与我相关
我的任务
分享
class _Girl
{
public:
class _Boy
{
public:
_Boy()
{
/*要求只能在这里获取_Girl的this指针
然后_Boy就可以对_Girl为所欲为了
哈哈哈
求相关代码
*/
}
}Boy;
};
int main()
{
_Girl Girl;
}
_Girl* pthis = (_Girl*)(this - &((_Girl*)nullptr)->Boy);
如果我把(char*)和(unsigned int)去掉会不会作死呢,试着运行了一遍好像没发生什么bug[/quote]
你这个例子 boy 和 girl 的地址本来就是一样的, &((_Girl*)nullptr)->Boy 的结果是 0, 所以看起来是一样的. 如果类 _Girl 还有其他数据程序在 boy 的前面, 去掉 (char*) 就会出问题了. 指针的加减法是加的所指向的数据类型的大小. _Girl* pthis = (_Girl*)(this - &((_Girl*)nullptr)->Boy);
如果我把(char*)和(unsigned int)去掉会不会作死呢,试着运行了一遍好像没发生什么bug
_Girl* pthis = (_Girl*)((char*)this - (unsigned int)&((_Girl*)nullptr)->Boy);