如何为二维数组的包装类重载数组索引运算符?

发布于 2024-08-16 02:19:59 字数 283 浏览 5 评论 0原文

#define ROW 3
#define COL 4

class Matrix
{
   private:
      int mat[ROW][COL];  
    //.....
    //.....

};

int main()
{
  Matrix m;
  int a = m[0][1]; //  reading
  m[0][2] = m[1][1]; // writing
} 

我直接认为不可能重载 [][] 。

我想我必须间接地做到这一点,但如何实施呢?

#define ROW 3
#define COL 4

class Matrix
{
   private:
      int mat[ROW][COL];  
    //.....
    //.....

};

int main()
{
  Matrix m;
  int a = m[0][1]; //  reading
  m[0][2] = m[1][1]; // writing
} 

I think directly it not possible to overload [][] .

I think i have to do it indirectly but how to implement it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜灵血窟げ 2024-08-23 02:19:59

更简单的解决方案是使用operator(),因为它允许多个参数。

class M
{
    public:
       int& operator()(int x,int y)  {return at(x,y);}
    // .. Stuff to hold data and implement at()
};


M   a;
a(1,2) = 4;

简单的方法是第一个运算符[]返回一个中间对象,第二个运算符[]返回数组中的值。

class M
{
    public:
    class R
    {
         private:
             friend class M; // Only M can create these objects.
             R(M& parent,int row): m_parent(parent),m_row(row) {}
         public:
              int& operator[](int col) {return m_parent.at(m_row,col);}
         private:
              M&  m_parent;
              int m_row;
    };

    R operator[](int row) {return R(*this,row);}

    // .. Stuff to hold data and implement at()
};

M   b;
b[1][2] = 3;   // This is shorthand for:

R    row = b[1];
int& val = row[2];
val      = 3;

The easier solution is to use the operator() as it allows multiple parameters.

class M
{
    public:
       int& operator()(int x,int y)  {return at(x,y);}
    // .. Stuff to hold data and implement at()
};


M   a;
a(1,2) = 4;

The easy way is that the first operator[] returns an intermediate object that the second operator[] returns the value from the array.

class M
{
    public:
    class R
    {
         private:
             friend class M; // Only M can create these objects.
             R(M& parent,int row): m_parent(parent),m_row(row) {}
         public:
              int& operator[](int col) {return m_parent.at(m_row,col);}
         private:
              M&  m_parent;
              int m_row;
    };

    R operator[](int row) {return R(*this,row);}

    // .. Stuff to hold data and implement at()
};

M   b;
b[1][2] = 3;   // This is shorthand for:

R    row = b[1];
int& val = row[2];
val      = 3;
琉璃梦幻 2024-08-23 02:19:59

由于您想将元素存储在固定大小的数组中,这将相当简单:

#define ROWS 3
#define COLS 4

typedef int row_type[COLS];

class matrix {
   row_type elements[ROWS];
public:
   ...
   row_type const& operator[](int r) const {return elements[r];}
   row_type      & operator[](int r)       {return elements[r];}
   ...
};

这应该可行。

此外,您可能需要用适当的常量替换#define,或者使用类型 (int) 和大小 (3x4) 的模板参数,以使您的矩阵类更加通用。如果你想支持动态大小,你的operator[]需要返回代理对象。这是可能的,但您可能应该更喜欢使用带有两个索引参数的operator()来进行元素访问。

Since you want to store your elements in fixed-size arrays it'll be fairly easy:

#define ROWS 3
#define COLS 4

typedef int row_type[COLS];

class matrix {
   row_type elements[ROWS];
public:
   ...
   row_type const& operator[](int r) const {return elements[r];}
   row_type      & operator[](int r)       {return elements[r];}
   ...
};

That should work.

In addition, you may want to replace the #defines with proper constants or use template parameters for type (int) and size (3x4) to make your matrix class more generic. If you want to support dynamic sizes your operator[] needs to return proxy-objects. It's possible but you should probably prefer operator() with two index parameters for element access.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文