在c ++中使用"This"指针

Jas*_*ick 1 c++ pointers this

我一直在阅读各种网站上的"this"指针(例如MSDN手册)并了解它的基本用法 - 返回一个副本你自己的对象或使用它的指针进行返回/比较.

但我发现了这句话:

  // Return an object that defines its own operator[] that will access the data.
  // The temp object is very trivial and just allows access to the data via 
  // operator[]
  VectorDeque2D_Inner_Set<T> operator[](unsigned int first_index) { 
    return VectorDeque2D_Inner_Set<T>(*this, first_index);
  }
Run Code Online (Sandbox Code Playgroud)

那是做什么的?它以某种方式增加了这个运算符,如果是这样,为什么?

(这来自我在堆栈溢出时给出的示例,因此语法中可能存在错误.请告诉我是否需要更大的块,我可以粘贴更多代码.)


编辑1
以下是整个列表,了解更多信息.该函数靠近类的底部.注意我将变量从x重命名为index并重命名了模板化的内部类.我忘了将类型转换为模板化的内部类,我在此更新中添加了它.

现在有什么想法?

template <typename T>
class Container
{
private:
    // ...


public:

    // Proxy object used to provide the second brackets
    template <typename T>
    class OperatorBracketHelper
    {
        Container<T> & parent;
        size_t firstIndex;
    public:
        OperatorBracketHelper(Container<T> & Parent, size_t FirstIndex) : parent(Parent), firstIndex(FirstIndex) {}

        // This is the method called for the "second brackets"
        T & operator[](size_t SecondIndex)
        {
            // Call the parent GetElement method which will actually retrieve the element
            return parent.GetElement(firstIndex, SecondIndex);
        }

    }

    // This is the method called for the "first brackets"
    OperatorBracketHelper<T> operator[](size_t FirstIndex)
    {
        // Return a proxy object that "knows" to which container it has to ask the element
        // and which is the first index (specified in this call)
        return OperatorBracketHelper<T>(*this, FirstIndex);
    }

    T & GetElement(size_t FirstIndex, size_t SecondIndex)
    {
        // Here the actual element retrieval is done
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

Ran*_*pho 5

*运营商取消引用this指针.这是必要的,因为被调用的方法(构造函数OperatorBracketHelper(Container<T> & Parent, size_t FirstIndex))需要引用而不是指针.

这是一种失败模式吗?我不知道.它本能地对我有气味,但我找不到任何直接错误的东西.