tags:

views:

121

answers:

3

I've been reading about the "this" pointer on various sites (e.g. the MSDN manuals) and understand its basic uses -- returning a copy your own object or using a pointer of it for returns/comparison.

But I came across this statement:

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

What does that do? Does it somehow increment the this operator, and if so, why??

(This comes from an example I was given on stack overflow, so there may be mistakes in the syntax. Let me know if a bigger chunk is necessary, I can paste more code in.)


EDIT 1
Here's the entire listing, for more info. The function is near the bottom of the class. Note I renamed the variable from x to index and renamed the templated inner class. I forgot to put the typecast to the templated inner-class, which I have added in this update.

Any ideas now?

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
        // ...
    }
}
+3  A: 

The * operator dereferences the this pointer. This is necessary because the method being called ( constructor OperatorBracketHelper(Container<T> & Parent, size_t FirstIndex) ) requires a reference rather than a pointer.

Is this a fail pattern? I dunno. It smells to me on an instinctive level, but I can't find anything directly wrong with it.

Randolpho
+1  A: 

it creates an instance of OpBracketHelper with the a reference to the current Container as its parent member (*this passes in a reference to the container object to the constructor)

My only concern would be about lifetimes of the Container and the helper object. I would be tempted to use shared_ptr rather than a reference

pm100
+2  A: 

The this keyword basically is a pointer reference to the object that it's currently in use. In C++, this is a pointer, so to dereference it, use *this.

So, this code,

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

returns a new VectorDeque2D_Inner_Set by passing a dereferenced of itself (since the constructor wants the reference of the object and not the pointer address).

This method,

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

just passed a dereferenced self to the constructor OperatorBracketHelper as it requires a Container& as parameter.

The Elite Gentleman
Make sense. What's the lifespan of the new object created, since it was returned from a function? Is there any worries there?
Jason R. Mick
As long as the object exists in memory (and not garbage collected, this should be fine). Oh yes, in C++, manage your pointers carefully as you don't want to have `Segmentation Faults`. In your example, you shouldn't worry at all, as you are getting OperatorBracketHelper from Container (which in essence means that Container exists in memory).
The Elite Gentleman
Thanks, good info. I'm picking your answer. Everyone gave good responses, but I felt yours was the most detailed.
Jason R. Mick
Glad to have been of help. Last time I did C++ was in 2006. :-)
The Elite Gentleman