我一直在阅读有关各种网站上的“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
// ...
}
}
给出的例子在哪里,上下文是什么? – 2010-09-23 17:41:53
尽管代码可能会编译,但它看起来是错误的。 – ybungalobill 2010-09-23 17:45:05
我已经添加了完整列表。当然,这并不是完整的代码,只是一个想法,这里给出了一些想法,关于通过中间内部对象使用双括号(“[] []”)访问2D包装矢量/双端对象。我很遗憾忘记我的原始表达式中的类型转换,并且没有把完整的列表,但至少我有意思问是否有必要! :) – 2010-09-23 17:46:50