2010-09-23 62 views
1

我一直在阅读有关各种网站上的“this”指针(例如MSDN手册)并理解其基本用法 - 返回一个拷贝自己的对象或使用它的指针用于退货/比较。在C++中使用“this”指针

但我遇到了这个说法:

// 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); 
    } 

是什么呢?它以某种方式增加这个操作符,如果是这样,为什么?

(这来自我的堆栈溢出给出的例子,所以有可能在语法错误。让我知道,如果一个更大的一块是必要的,我可以粘贴在更多的代码。)


编辑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 
     // ... 
    } 
} 
+0

给出的例子在哪里,上下文是什么? – 2010-09-23 17:41:53

+0

尽管代码可能会编译,但它看起来是错误的。 – ybungalobill 2010-09-23 17:45:05

+0

我已经添加了完整列表。当然,这并不是完整的代码,只是一个想法,这里给出了一些想法,关于通过中间内部对象使用双括号(“[] []”)访问2D包装矢量/双端对象。我很遗憾忘记我的原始表达式中的类型转换,并且没有把完整的列表,但至少我有意思问是否有必要! :) – 2010-09-23 17:46:50

回答

4

this关键字基本上是指向它当前正在使用的对象的指针引用。在C++中,这个是一个指针,所以要取消引用它,请使用*this

所以,这个代码,

return VectorDeque2D_Inner_Set<T>(*this, index); 

通过传递一个间接引用的本身(因为构造希望对象的引用,而不是指针地址)返回一个新VectorDeque2D_Inner_Set。

这种方法,

// 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); 
    } 

刚刚通过了一项解除引用self给构造OperatorBracketHelper,因为它需要一个Container&作为参数。

+0

有道理。由于它是从函数返回的,所以创建的新对象的寿命是多少?那里有什么问题吗? – 2010-09-23 17:58:12

+0

只要对象存在于内存中(而不是垃圾收集,这应该很好)。哦,是的,在C++中,小心管理你的指针,因为你不想有'Segmentation Faults'。在你的例子中,你不应该担心,因为你从Container获得OperatorBracketHelper(实质上意味着Container存在于内存中)。 – 2010-09-23 18:02:23

+0

谢谢,很好的信息。我正在选择你的答案。每个人都给予了很好的回应,但我觉得你是最详细的。 – 2010-09-23 18:32:41

5

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

这是失败模式吗?我不知道。它本能地让我闻起来,但我找不到任何直接的错误。

1

它与当前的容器作为其父构件(*这通过在一个参考容器对象的构造函数)的一个参考创建OpBracketHelper的实例

我唯一担心将是约的寿命容器和辅助对象。我会试图使用shared_ptr而不是参考