<i id='CYYUZ'><tr id='CYYUZ'><dt id='CYYUZ'><q id='CYYUZ'><span id='CYYUZ'><b id='CYYUZ'><form id='CYYUZ'><ins id='CYYUZ'></ins><ul id='CYYUZ'></ul><sub id='CYYUZ'></sub></form><legend id='CYYUZ'></legend><bdo id='CYYUZ'><pre id='CYYUZ'><center id='CYYUZ'></center></pre></bdo></b><th id='CYYUZ'></th></span></q></dt></tr></i><div id='CYYUZ'><tfoot id='CYYUZ'></tfoot><dl id='CYYUZ'><fieldset id='CYYUZ'></fieldset></dl></div>

<small id='CYYUZ'></small><noframes id='CYYUZ'>

  • <tfoot id='CYYUZ'></tfoot>

    <legend id='CYYUZ'><style id='CYYUZ'><dir id='CYYUZ'><q id='CYYUZ'></q></dir></style></legend>

        • <bdo id='CYYUZ'></bdo><ul id='CYYUZ'></ul>
      1. C++ 重载运算符 [ ][ ]

        时间:2023-09-18
        • <tfoot id='rQiDX'></tfoot>

          1. <legend id='rQiDX'><style id='rQiDX'><dir id='rQiDX'><q id='rQiDX'></q></dir></style></legend>

              <tbody id='rQiDX'></tbody>
              • <bdo id='rQiDX'></bdo><ul id='rQiDX'></ul>
                1. <small id='rQiDX'></small><noframes id='rQiDX'>

                  <i id='rQiDX'><tr id='rQiDX'><dt id='rQiDX'><q id='rQiDX'><span id='rQiDX'><b id='rQiDX'><form id='rQiDX'><ins id='rQiDX'></ins><ul id='rQiDX'></ul><sub id='rQiDX'></sub></form><legend id='rQiDX'></legend><bdo id='rQiDX'><pre id='rQiDX'><center id='rQiDX'></center></pre></bdo></b><th id='rQiDX'></th></span></q></dt></tr></i><div id='rQiDX'><tfoot id='rQiDX'></tfoot><dl id='rQiDX'><fieldset id='rQiDX'></fieldset></dl></div>
                  本文介绍了C++ 重载运算符 [ ][ ]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有 CMatrix 类,其中是指向值数组的双指针".

                  I have class CMatrix, where is "double pointer" to array of values.

                  class CMatrix {
                  public:
                      int rows, cols;
                      int **arr;
                  };
                  

                  我只需要通过键入以下内容来访问矩阵的值:

                  I simply need to access the values of matrix by typing:

                  CMatrix x;
                  x[0][0] = 23;
                  

                  我知道如何使用:

                  x(0,0) = 23;
                  

                  但我真的需要以另一种方式做到这一点.任何人都可以帮助我吗?请?

                  But I really need to do that the other way. Can anyone help me with that? Please?

                  最后感谢大家的帮助,我是这样做的...

                  Thank you guys for help at the end I did it this way...

                  class CMatrix {
                  public:
                     int rows, cols;
                     int **arr;
                  
                  public:
                     int const* operator[]( int const y ) const
                     {
                        return &arr[0][y];
                     }
                  
                     int* operator[]( int const y )
                     {
                        return &arr[0][y];
                     }
                  
                     ....
                  

                  感谢您的帮助,我真的很感激!

                  Thank you for your help I really appreciate it!

                  推荐答案

                  你不能重载operator [][],但这里常用的习惯用法是使用代理类, 即重载 operator [] 以返回重载了 operator [] 的不同类的实例.例如:

                  You cannot overload operator [][], but the common idiom here is using a proxy class, i.e. overload operator [] to return an instance of different class which has operator [] overloaded. For example:

                  class CMatrix {
                  public:
                      class CRow {
                          friend class CMatrix;
                      public:
                          int& operator[](int col)
                          {
                              return parent.arr[row][col];
                          }
                      private:
                          CRow(CMatrix &parent_, int row_) : 
                              parent(parent_),
                              row(row_)
                          {}
                  
                          CMatrix& parent;
                          int row;
                      };
                  
                      CRow operator[](int row)
                      {
                          return CRow(*this, row);
                      }
                  private:
                      int rows, cols;
                      int **arr;
                  };
                  

                  这篇关于C++ 重载运算符 [ ][ ]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:获取由向量的向量表示的矩阵的第一列 下一篇:如何将简单指针转换为固定大小的多维数组?

                  相关文章

                  最新文章

                    <tfoot id='jrlFq'></tfoot>
                    • <bdo id='jrlFq'></bdo><ul id='jrlFq'></ul>
                    <i id='jrlFq'><tr id='jrlFq'><dt id='jrlFq'><q id='jrlFq'><span id='jrlFq'><b id='jrlFq'><form id='jrlFq'><ins id='jrlFq'></ins><ul id='jrlFq'></ul><sub id='jrlFq'></sub></form><legend id='jrlFq'></legend><bdo id='jrlFq'><pre id='jrlFq'><center id='jrlFq'></center></pre></bdo></b><th id='jrlFq'></th></span></q></dt></tr></i><div id='jrlFq'><tfoot id='jrlFq'></tfoot><dl id='jrlFq'><fieldset id='jrlFq'></fieldset></dl></div>

                    1. <legend id='jrlFq'><style id='jrlFq'><dir id='jrlFq'><q id='jrlFq'></q></dir></style></legend>

                      <small id='jrlFq'></small><noframes id='jrlFq'>