views:

133

answers:

3

I was wondering how can you use operators along with 'this'. To put an example:

class grd : clm
{ 
    inline int &operator()(int x, int y) { return g[x][y]; }
    /* blah blah blah */ 
};

grd grd::crop(int cx1, int cy1, int cx2, int cy2)
{
    grd ngrd(abs(cx1-cx2), abs(cy1-cy2));
    int i, j;
    //
    for (i = cx1; i < cx2; i++)
    {
        for (j = cy1; j < cy2; j++)
        {
            if (i >= 0 && i < x && j >= 0 && j < y)
                ngrd(i-cx1,j-cy2) = ?? // this->(i,j); ****
        }
    }
    return ngrd;
}

Thanks!

+6  A: 

Just do

(*this)(i,j);
Martin B
Oh, dereference... the solution is always easier than expected :) Thx!
huff
+8  A: 

Either: this->operator()(i,j) or (*this)(i,j)

Joe Gauterin
+1  A: 

I also often will do things like this:

grd& This(*this);
This(i,j);

It's just the same as the above examples, but it gets rid of the extra pointer notation and can make the code look cleaner.

miked