两个方括号超载

Two square bracket overloading

本文关键字:方括号 超载 两个      更新时间:2023-10-16

我正在用c++编写一个矩阵类,并试图重载一些操作符,如=和>>和<<等。

我无法重载矩阵类的运算符[][]。如果我有一个像M1这样的类矩阵对象,那么我可以用这种方式给每个元素赋值:

M1[1][2]=5;

int X;
X=M1[4][5];

只需重载operator[]并使其返回指向矩阵的相应行或列的指针。由于指针支持[]下标,因此可以使用'双平方'表示法[][]进行访问。

也可以用两个参数重载operator()

c++中没有operator[][]。你必须return一个助手对象,然后重载operator[],也有这种访问。

您可以重载operator[]。因此,如果你想用这种方式使用矩阵,你应该把矩阵作为向量数组。

class Matrix
{
...
  Vector & operator[]( int index );
...
};

class Vector
{
...
  double & operator[]( int index );
...
};
最后

:

Matrix m;
...
double value = m[i][j];
...

没有operator[][],您可以实现operator[]来返回对行/列对象的引用,其中您可以实现operator[]来返回对单元格的引用。

你可以做以下的事情来避免所有的麻烦。

struct loc
{
  int x;
  int y;
};

然后在你的operator[]过载中,接受一个loc,比如

T& operator[](loc const& cLoc)
{
 // now you have x/y you can return the object there.
}
要调用

,可以简单地这样做:

matrix[loc(2,3)] = 5; 

实际上,几年前我在自己的矩阵类中就这样做了。在本例中,我定义了一个矩阵模板类,它包含下面的代码片段。

然后我就可以像下面这样迭代和赋值了:

          for(size_t k=1; k<n; ++k) {       
                   minor[p][k-1]=major[j][k];        
           }

我希望这对你有帮助。

// //////////////////////////////////////////////////////////////////////////////
// list is internal vector representation of n x m matrix
T* list;
// Proxy object used to provide the column operator
template < typename T >
class OperatorBracketHelper
{
    Matrix < T > & parent ;
    size_t firstIndex ;
    public :
        OperatorBracketHelper ( Matrix < T > & Parent , size_t FirstIndex ) : 
        parent ( Parent ), firstIndex ( FirstIndex ) {}
        // method called for column operator
        T & operator []( size_t SecondIndex )
        {
            // Call the parent GetElement method which will actually retrieve the element
            return parent.GetElement ( firstIndex , SecondIndex );
        }
};
// method called for row operator
OperatorBracketHelper < T > operator []( size_t FirstIndex )
{
    // Return a proxy object that "knows" to which container it has to ask the element
    // and which is the first index (specified in this call)
    return OperatorBracketHelper < T >(* this , FirstIndex );
}
T & GetElement ( size_t FirstIndex , size_t SecondIndex )
{
    return list[FirstIndex*cols+SecondIndex];
}

我确实在一个矩阵类上工作,我决定首先创建一个具有动态二维数组的数组类。就像你们一样,我遇到了这个问题如何重载两个方括号。我处理这个案子的方法很简单;我重载了两次方括号操作符作为成员函数。首先,我重载了[],以便返回一个指向所需行的指针,可以这么说,然后,下面的成员函数(即再次重载operator[])返回与数组元素相同类型的左值。

但是,请注意,为调用前一个重载操作符[]而调用的索引必须保存在某个地方,以便可以在后一个重载操作符[]中使用它。出于这个原因,我只是在类Array中添加了一个int类型的新成员(在下面的代码中我将其命名为"test")。

class Array {
private: 
 double **ptr; int test;
 ...   /* the rest of the members includes the number of rows and columns */
public:
    Array(int=3,int=3);  // Constructor
    Array(Array &);      // Copy Constructor
    ~Array();            // Destructor
    void get_array();
    void show_array();
    double* operator[] (int);
    double operator[] (short int);
    ...
};
... 
double* Array::operator[] (int a) {
    test = a;
    double* p = ptr[test];
    return p;
}
double Array::operator[] (short int b) {
   return ((*this)[test][b]);
}
因此,作为一个例子,在main中我可以简单地写:
int main(){
Array example;
cout << example[1][2];  
}

希望这对你有帮助。

您不能这样重载[][],因为没有这样的操作符。你可以重载[]来返回一些东西在其上定义了一个[](代理);在最简单的情况下,像double*这样的东西也可以,但通常更好,虽然多了一点工作,但要使用一个完整的类。(添加的地方例如,边界检查。)

也可以重载(x,y)。取决于你是谁问一下,哪种格式"更好"。(事实上,它是)

模板矩阵类

template <uint8_t rows, uint8_t cols, typename T>
class MatrixMxN {
public:
    T* operator[](uint8_t f_row) {
        return &m_val[f_row * cols];
    }
protected:
    T m_val[rows*cols];
};

,这里是3行4列整数型矩阵的对象。

MatrixMxN<3, 4, int32_t> M;
M[2][3] = 10;
std::cout << M[2][3] << std::endl;