Why cant be overloaded
When you have multiple subscripts, the cleanest way to do it is with operator rather than with operator[]. The reason is that operator[] always takes exactly one parameter, but operator can take any number of parameters in the case of a rectangular matrix, two parameters are needed. Then you can access an element of Matrix m using m i,j rather than m[i][j] :. See the next FAQ for more detail on the reasons to use m i,j vs.
Thus they access elements of the matrix using syntax like m[i][j] rather than syntax like m i,j. The array-of-array solution obviously works, but it is less flexible than the operator approach. Specifically, there are easy performance tuning tricks that can be done with the operator approach that are more difficult in the [][] approach, and therefore the [][] approach is more likely to lead to bad performance, at least in some cases. In contrast, the operator approach totally hides the physical layout of the matrix, and that can lead to better performance in some cases.
Put it this way: the operator approach is never worse than, and sometimes better than, the [][] approach. As an example of when a physical layout makes a significant difference, a recent project happened to access the matrix elements in columns that is, the algorithm accesses all the elements in one column, then the elements in another, etc. Of course there are many examples of this sort of thing from numerical methods, and sparse matrices are a whole other dimension on this issue.
Use the operator approach. The same reasons you encapsulate your data structures, and the same reason you check parameters to make sure they are valid. A few people use [][] despite its limitations , arguing that [][] is better because it is faster or because it uses C-syntax.
Plus, oh yea, the C-syntax makes it harder to change the data structure and harder to check parameter values. The point of the previous two FAQs is that m i,j gives you a clean, simple way to check all the parameters and to hide and therefore, if you want to, change the internal data structure. The world already has way too many exposed data structures and way too many out-of-bounds parameters, and those cost way too much money and cause way too many delays and way too many defects.
Now everybody knows that you are different. For them, maintenance costs are high, defects are real, and requirements change. Believe it or not, every once in a while they need to better sit down change their code.
Admittedly my thongue wath in my theek. But there was a point. The point was that encapsulation and parameter-checking are not crutches for the weak. The m i,j syntax is one of those techniques. Fortunately you are not average, so read on.
Beware that this can slow down your program. Next you will enable const overloading by repeating the above steps. You will create the const version of the various methods, and you will create a new nested class, probably called Matrix::ConstRow.
Final step: find the joker who failed to read the previous FAQ and thonk him in the noggin. If you have a decent compiler and if you judiciously use inlining , the compiler should optimize away the temporary objects.
In other words, the operator[] -approach above will hopefully not be slower than what it would have been if you had directly called Matrix::operator unsigned row, unsigned col in the first place. Of course you could have made your life simpler and avoided most of the above work by directly calling Matrix::operator unsigned row, unsigned col in the first place.
So you might as well directly call Matrix::operator unsigned row, unsigned col in the first place. A good interface provides a simplified view that is expressed in the vocabulary of a user.
In the case of OO software, the interface is normally the set of public methods of either a single class or a tight group of classes. First think about what the object logically represents, not how you intend to physically build it. For example, suppose you have a Stack class that will be built by containing a LinkedList :. Should the Stack have a get method that returns the LinkedList?
Or a set method that takes a LinkedList? Or a constructor that takes a LinkedList? Obviously the answer is No, since you should design your interfaces from the outside-in. Now for another example that is a bit more subtle.
Suppose class LinkedList is built using a linked list of Node objects, where each Node object has a pointer to the next Node :. Should the LinkedList class have a get method that will let users access the first Node? Should the Node object have a get method that will let users follow that Node to the next Node in the chain?
Related Articles. Table of Contents. Save Article. Improve Article. Like Article. Display ;. GFG int j. Recommended Articles. Article Contributed By :. Ask a Question. Why some operators like :: ,. Please Sign up or sign in to vote. Copy Code. Updated Oct pm OriginalGriff. Add a Solution. Richard MacCutchan Oct am. Because those are the rules of the language. You can read the full explanation in any of the published documentation. Top Rated Most Recent. Accept Solution Reject Solution.
Similarly, ". Dot specifies a member of a class, and star is a pointer resolution operator. Posted Oct am OriginalGriff. But you are probably referring to the unary operator, not the binary one ;-p P.
0コメント