Overloading array operator

This is a discussion on Overloading array operator within the C++ Programming forums, part of the General Programming Boards category; Hello all, I know it is possible to overload the bracket symbol '[]', and I know how to do it. ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    64

    Overloading array operator

    Hello all,

    I know it is possible to overload the bracket symbol '[]', and I know how to do it. I was wondering, is it possible to overload the double-bracket symbol? I have a matrix class. I am doing the following:

    Code:
    class Matrix
    {
       ...
       double** operator[][](int&, int&);
       ...
    };
    Does this seem right, such that I can do something like the following in my main program?

    Code:
    Matrix A;
    ... //assign some values
    cout<<A[2][2];
    Thanks.

    Bill

  2. #2
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Compile and see.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    64
    Sorry I forgot to mention that I did compile and I am getting errors. Here is all the code:

    Code:
    //#include "Complex.h"
    
    class Matrix
    {
    	int row,col;
    	Complex **A;
    	bool empty;
    public:
    	Matrix();
    	Matrix(const int&, const int&);
    	~Matrix();
    	Matrix operator*(const Matrix&);
    	Complex** operator[][](int&, int&);
    };
    
    Matrix::Matrix()
    {
    	row = col = 0;
    	empty = true;
    }
    
    Matrix::Matrix(const int &a, const int &b)
    {
    	row = a;
    	col = b;
    
    	empty = false;
    
    	A = new Complex* [row];
    	for (int i=0; i<row; i++)
    		A[i] = new Complex[col];
    	
    	for (i=0; i<row; i++)
    	{
    		for(int j=0; j<col; j++)
    		{
    			A[i][j] = 0;		//zero-out the entire matrix;
    		}
    	}
    }
    
    Matrix::~Matrix()
    {
    	if(!empty)
    	{
    		for (int i=0; i<row; i++)
    			delete[] A[i];
    		delete[] A;
    	}
    }
    
    Complex** Matrix::operator[][](int &a, int &b)
    {
    	return A[a][b];
    }
    
    Matrix Matrix::operator*(const Matrix &another)
    {
    	//still didn't do this one
    }
    The following errors occur pointing to the function prototype and the first bracket of the function definition:
    Code:
    Error C2092: array element type cannot be function
    The last error says "cannot recover from previous errors; stopping compliation."

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> is it possible to overload the double-bracket symbol?

    No. You have to overload operator[] to return something that has its own operator[] overloaded to return a value. It is somewhat complicated.

    You can also just use a regular function that takes two parameters.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Operator Overloading
    By Bnchs in forum C++ Programming
    Replies: 2
    Last Post: 11-29-2006, 12:26 PM
  2. operator overloading
    By blue_gene in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2004, 05:06 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM
  5. Overloading the [] operator
    By Strahan in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2002, 03:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21