Jason R. Mick
Jason R. Mick

Reputation: 5297

Using the "This" Pointer in c++

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
        // ...
    }
}

Upvotes: 1

Views: 8863

Answers (3)

Buhake Sindi
Buhake Sindi

Reputation: 89209

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.

Upvotes: 4

pm100
pm100

Reputation: 50210

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

Upvotes: 1

Randolpho
Randolpho

Reputation: 56448

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.

Upvotes: 5

Related Questions