Home arrow C++ arrow Page 7 - Operator Overloading in C++

Operator Overloading in C++

Operating overloading allows you to pass different variable types to the same function and produce different results. In this article Ben gives us the low-down on operator overloading in C++.

Author Info:
By: Ben Watson
Rating: 3 stars3 stars3 stars3 stars3 stars / 370
December 08, 2002
  1. · Operator Overloading in C++
  2. · Definition
  3. · Overloading =
  4. · Overloading
  5. · Matrix Multiplication - Overloading * Again
  6. · Putting It All Together
  7. · Conclusion

print this article

Operator Overloading in C++ - Conclusion
(Page 7 of 7 )

Operator overloading can be a powerful programming tool when it increases usability and understandability of a class. In this case, it's much nicer to write matrix[0][4] rather than matrix->GetRow(0)->GetCol(4). However, it must always be remembered that overloading serves to make programming easier--it doesn't benefit the end user at all. Overloading an operator must make sense, as I hope all of the decisions I made in this class make sense. If it's appropriate, go ahead and use it. If it will make people wonder what you meant, than it's probably something to stay away from.

Also, because this is a template, you could possibly have a matrix of any object, including, for example strings. This will work perfectly in some situations, but not in others. Basically, if + is defined on strings, then you can do matrix addition on a string matrix. However, * is usually not defined for strings, so a statement with that would refuse to compile.

Matrix<string> s(2,7);
Matrix<string> t(2,7);

Matrix<string> u=s+t;//will work fine--concatenates strings at same locations in matrix

Matrix<string> v(7,5);

Matrix<string> w=s*v;//will not compile:error C2676: binary '*' : 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator

I did not make use of the -- and ++ unary operators in this tutorial because they don't always make sense when used on a matrix. With these operators it is important to know that since they can be both prefix and postfix, each version has a slightly different function signature. With the understanding you have gained in this tutorial, you should be able to look up how to implement these operators without trouble.

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials