*
*
(Click below for a an individual note from Marshall Cline.)


You are watching: Does not provide a subscript operator

13.1
What's the deal with operator overloading?
13.2 What are the benefits of operator overloading?
13.3 What room some instances of operator overloading?
13.4 Is operator overloading an alleged to do the class' password clearer?
13.5 What operator can/cannot it is in overloaded?
13.6 Overloading operator== to usage string comparison?
13.7 Can I develop operator** because that to-the-power-of?
13.8 But which operator should ns override?
13.9 Guidelines / "rules of thumb" because that overloading operators?
13.10 Creating a subscript operator for a matrix class?
13.11 Should mine Matrix interface look choose an array-of-array?
13.12 Part two of making mine Matrix interface look favor an array-of-array?
13.13 Designing classes from the outside-in vs. Inside-out?
13.14 Overloading prefix/postfix develops of operators ++ and also --?
13.15 Which is an ext efficient: i++ or ++i?

*
E-mail the author modification Jul 4, 2012


See more: How Does An F+ Cell Differ From An Hfr Cell? Mastering Microbiology Flashcards

Use operator() quite than operator<>.When you have actually multiple subscripts, the cleanest means to execute it is withoperator() rather than through operator<>. The reason is thatoperator<> constantly takes precisely one parameter, however operator()can take any number of parameters (in the instance of a rectangle-shaped matrix, twoparameters are needed).For example:class procession public: Matrix(unsigned rows, unsigned cols); double& operator() (unsigned row, unsigned col); ← subscript operators regularly come in pairs dual operator() (unsigned row, unsigned col) const; ← subscript operators frequently come in pairs ... ~Matrix(); // Destructor Matrix(Matrix const& m); // Copy constructor Matrix& operator= (Matrix const& m); // Assignment operator ...private: unsigned rows_, cols_; double* data_;;inlineMatrix::Matrix(unsigned rows, unsigned cols) : rows_ (rows) , cols_ (cols) //data_ = rows_ || col >= cols_) litter BadIndex("Matrix subscript out of bounds"); return data_;}inlinedouble Matrix::operator() (unsigned row, unsigned col) constThen you can accessibility an element of matrix m making use of m(i,j)rather 보다 m:int main(){ matrix m(10,10); m(5,8) = 106.15; std::cout view the following FAQ for much more detail top top thereasons to use m(i,j) vs. M.