Two square bracket overloading

22,164

Solution 1

Just overload operator[] and make it return a pointer to the respective row or column of the matrix. Since pointers support subscripting by [], access by the 'double-square' notation [][] is possible then.

You can also overload operator() with two arguments.

Solution 2

There is no operator[][] in C++. You have to return a helper object and then overload operator[] for that too, to have this kind of access.

Solution 3

You could overload operator[]. So if you would like to use matrix that way, you should make matrix as array of vectors.

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

and

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

Finally:

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

Solution 4

there is no operator[][], you can implement operator[] to return a reference to the row/column object, in which you can implement the operator[] to return you the cell reference.

You can do something like the following to avoid all that hassle..

struct loc
{
  int x;
  int y;
};

then in your operator[] overload, accept a loc, something like

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

To call, you can simply do something like:

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

Solution 5

Actually, I did just that in my own matrix class a few years ago. In this case, I defined a matrix template class that contained the snippet, below.

I was then able to iterate and assign as follows:

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

I hope this helps.

// //////////////////////////////////////////////////////////////////////////////
// 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];
}
Share:
22,164

Related videos on Youtube

fafa
Author by

fafa

Updated on October 05, 2020

Comments

  • fafa
    fafa over 3 years

    I am writing a matrix class in c++ and trying to overload some operator like = and >> and << etc.

    I was unable to overload operator [][] for matrix class. if i have an object of class matrix like M1 then i can use this way for giving value to each element:

    M1[1][2]=5;
    

    OR

    int X;
    
    X=M1[4][5];
    
  • Björn Pollex
    Björn Pollex about 13 years
    I prefer RedX's solution over this one because in my opinion you should not use pointers in C++ unless you absolutely have to.
  • edA-qa mort-ora-y
    edA-qa mort-ora-y about 13 years
    I find that operator(x,y) is a good solution in such circumstances.
  • David Stone
    David Stone about 10 years
    This is an interesting idea I haven't seen before, but is still immediately obvious what it does.
  • Charlie Su
    Charlie Su about 6 years
    How do you overload operator() with two arguments? I get a compiler error saying that it "must take exactly one argument" when I try that.

Related