C++ C++;指向重载索引的箭头(此->;[])

C++ C++;指向重载索引的箭头(此->;[]),c++,operator-overloading,C++,Operator Overloading,我有一个简单的类,我重载了它的索引运算符: class dgrid{ double* data; // 1D Array holds 2D data in row-major format public: const int nx; const int ny; double* operator[] (const int index) {return &(data[index*nx]);} } 这样,dgrid[x][y]可以作为二维数组工作,但数据在

我有一个简单的类,我重载了它的索引运算符:

class dgrid{
    double* data; // 1D Array holds 2D data in row-major format
  public:
    const int nx;
    const int ny;
    double* operator[] (const int index) {return &(data[index*nx]);}
}
这样,
dgrid[x][y]
可以作为二维数组工作,但数据在内存中是连续的

然而,从内部成员函数来看,这有点笨重,我需要做一些类似于
(*this)[x][y]
的事情,这很有效,但似乎很难闻,尤其是当我有如下部分时:

(*this)[i][j] =   (*this)[i+1][j]
                + (*this)[i-1][j]
                + (*this)[i][j+1]
                + (*this)[i][j-1] 
                - 4*(*this)[i][j];

有更好的方法吗?类似于
this->[x][y]
(但这不起作用)。使用一个小函数
f(x,y)返回&数据[index*nx+ny]
是唯一的选择吗?

你可以重载->,但为什么不简单地做:

T& that = *this; //or use auto as t.c. suggests

that[i][j] =  that[i+1][j]
            + that[i-1][j]
            + that[i][j+1]
            + that[i][j-1] 
            - 4*that[i][j];

这(双关语)至少和这个一样可读->[]。否?

auto&self=*此;自我[i][j]=或者,对于更具冒险精神的类型,
(*这个)
可以写成
这个[0]
…更笨拙,但你所追求的也是:
这个->操作符[](i)[j]
:)谢谢MatiasFG,这实际上是我认为我在寻找的答案,尽管T.C./杰弗里的工作更整洁。