How do i develop a subscript operator for a matrix class?, C/C++ Programming

Employ operator () instead of operator[].

While you have multiple subscripts, the cleanest way to do it is along with operator () instead of with operator[]. The reason is that operator[] always takes specifically one parameter, however operator() can take any number of parameters (in the case of rectangular matrix, two parameters are required).

For instance:

class Matrix {

public:

Matrix(unsigned rows, unsigned cols);

double& operator() (unsigned row, unsigned col);  subscript operators frequently come in pairs double operator() (unsigned row, unsigned col) const;  subscript operators frequently come in pairs

...

~Matrix(); // Destructor

Matrix(const Matrix& m); // Copy constructor

Matrix& operator= (const Matrix& m); // Assignment operator

... private:

unsigned rows_, cols_;

double* data_;

};

inline

Matrix::Matrix(unsigned rows, unsigned cols)

: rows_ (rows)

, cols_ (cols)

//data_ <--initialized below (after the 'if/throw' statement)

{

if (rows == 0 || cols == 0)

throw BadIndex("Matrix constructor contain 0 size");

data_ = new double[rows * cols];

}

inline

Matrix::~Matrix()

{

delete[] data_;

}

inline

double& Matrix::operator() (unsigned row, unsigned col)

{

if (row >= rows_ || col >= cols_)

throw BadIndex("Matrix subscript is beyond bounds");

return data_[cols_*row + col];

}

inline

double Matrix::operator() (unsigned row, unsigned col) const

{

if (row >= rows_ || col >= cols_)

throw BadIndex("const Matrix subscript is beyond bounds");

return data_[cols_*row + col];

}

You can then access an element of Matrix m via m(i,j) instead of m[i][j]:

int main()

{

Matrix m(10,10); m(5,8) = 106.15; std::cout << m(5,8);

...

}

Posted Date: 3/16/2013 3:17:39 AM | Location : United States







Related Discussions:- How do i develop a subscript operator for a matrix class?, Assignment Help, Ask Question on How do i develop a subscript operator for a matrix class?, Get Answer, Expert's Help, How do i develop a subscript operator for a matrix class? Discussions

Write discussion on How do i develop a subscript operator for a matrix class?
Your posts are moderated
Related Questions
array of class objects and single inheritance

Program for Memory Optimization of C / OpenCL Code Project Description: This is possibly a very simple project for someone with an excellent knowledge of C and OpenCL. That,

Scope Resolution operator: The scope resolution operator: : is used to access global variable in the inner block. When global and local variable are identical name the scope re

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

Question: (a) (i) Explain how class members can be accessed when using objects of the class. Use a suitable example to illustrate your answer. (ii) Explain how the privat

Bit-wise Operators Some applications require operations to be done on dissimilar bits of a byte separately. Bit-wise operators offer a facility to do just that. There are vario

Explain the Declaration of Multi Dimensional Arrays? In the figure, the range in the first dimension is 3 and in the second dimension is 4. The shaded portion corresponds to th



For your class to work properly, you'll need to define appropriate constructors, extract and insert operators, and of course arithmetic operators. (If you wanted to use it as a gen