开发者

C++ 'this' and operator overloading

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

cl开发者_如何学Pythonass 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!


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


Just do

(*this)(i,j);


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜