这是一个指向调用对象的指针(它返回右值)。
*这是一个指向调用对象指针的指针(它返回地址的值)。
**这是一个指向调用对象(???)的指针的指针。
&***这是对调用对象指针(???)的指针的引用。
std::vector<int>:: iterator i = vector1.begin();
i 是指向它自己的右值的指针(返回它自己的值)。
*i 是 vector 中包含的对象的右值指针(返回 &value 中指向的值)。
**i 是指向 vector 中包含的对象的右值指针的指针 (???)。
我真的很困惑。
这是一个示例代码,我们在其中找到表达式 &**this:
class _Iter
{
private:
ListElem *pCurr;
const List *pList;
public:
_Iter(ListElem *pCurr, const List *list)
: pCurr_(pCurr), pList(list)
{}
T& operator*() { return pCurr_->data; }
T* operator->() { return &**this; }
};
最佳答案
this
是指向当前对象的指针。
*this
是对当前对象的引用,即 this
取消引用。
**this
是在当前对象上调用的重载一元 operator*
函数的返回值。
如果从 **this
返回的对象有一个重载的 operator&()
函数,那么 &**this
计算返回值(**this).operator&()
的。否则,&**this
是指向在当前对象上调用的重载一元 operator*
函数的返回值的指针。
例子:
#include <iostream>
struct A
{
int b;
int a;
int& operator*() {return a;}
int* test()
{
return &**this;
}
};
int main()
{
A a;
std::cout << "Address of a.a: " << a.test() << std::endl;
std::cout << "Address of a.a: " << &(*a) << std::endl;
std::cout << "Address of a.a: " << &(a.a) << std::endl;
return 0;
}
示例输出:
Address of a.a: 0x7fffbc200754
Address of a.a: 0x7fffbc200754
Address of a.a: 0x7fffbc200754
关于c++ - &**this 究竟返回了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22368300/