什么&**这个回归到底是什么?

use*_*016 11 c++ pointers

这是一个指向调用对象的指针(它返回r值).

*这是一个指向调用对象指针的指针(它返回地址的值).

**这是一个指向调用对象(???)指针的指针.

&***这是对调用对象(???)指针指针的引用.

std::vector<int>:: iterator i = vector1.begin();
Run Code Online (Sandbox Code Playgroud)

我是指向它自己的r值的指针(返回它自己的值).

*i是向量中包含的对象的r值的指针(返回&value中指向的值).

**i是指向包含在向量(???)中的对象的r值的指针.

我真的很困惑.

这是一个示例代码,我们在其中找到表达式&**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; }
};
Run Code Online (Sandbox Code Playgroud)

R S*_*ahu 20

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;
}
Run Code Online (Sandbox Code Playgroud)

样本输出:

Address of a.a: 0x7fffbc200754
Address of a.a: 0x7fffbc200754
Address of a.a: 0x7fffbc200754
Run Code Online (Sandbox Code Playgroud)

  • 看看这是否会使它更清晰(或更令人困惑):`**this`可以解释为`this - > operator*()`.因此`&**this`是`&(this-> operator*())`,它取当前对象的重载`*`运算符的返回值的地址:P (7认同)