Home arrow C++ arrow Page 3 - 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++ - Overloading =
(Page 3 of 7 )

The only other operator we need to overload is assignment (=). When overloading assignment, we must keep in mind that the object we're assigning to must already exist, and it is that object's operator= method which will be called.

Row& operator=(const Row& r) {
for (int i=0;i<numCols;i++)
return *this;

Again we return a reference, but this time it's a reference to itself. First we set the size of the current row equal to that of the source row, then we copy its values. There is an important note here. Notice that I'm using [] on the primitive T array itself--NOT the overloaded []s of Row. Remember that Row's [] returns a reference, thus if we had written row[i]=r[i], we would get a row that references the exact same data in memory, so that when we changed one the other would change--this isn't what we want at all, so we need to access the raw data in the Row class.

Now we can write code like this:

Row<double> r1(5);
Row<double> r2;//creates an empty row
Row<double> r3(2);
r3=r1;//overwrites previous row information to contain same info as r1

Matrices are Made of Many Rows
Now that we have a working Row, we can combine rows into a matrix. Let's start with this basic definition:

template<class T>
class Matrix {
Matrix(int rows=0, int cols=0): matrix(NULL) {
Matrix(const Matrix& m): matrix(NULL) {
for (int r=0;r<numRows;r++)
matrix[r]=Row<T>(m.matrix[r]);//assign to primitive array, NOT overloaded []--to get a copy
void SetSize(int rows, int cols) {
if (rows) delete[]matrix;
if (cols > 0 && rows >0) {
matrix=new Row<T>[rows];
for (int i=0;i<rows;i++)
int GetCols() { return numCols;}
int GetRows() { return numRows;}

int numCols, numRows;
Row<T>* matrix;


This follows very closely the basic form of the Row class. The only item of interest is when we declare and allocate a matrix: we must specify the type, T, after the class name.

First let's implement the same operators we did on the Row class:

Row<T>& operator[](int index) {
return matrix[index];

Matrix& operator=(const Matrix& m) {
for (int r=0;r<numRows;r++)
matrix[r]=Row(m.matrix[r]);//assign to primitive array, NOT overloaded []--to get a copy
return *this;

The most important part of this code is the return type of operator[]. It returns a reference to a Row of type T. This little fact allows us to use the Matrix class like this:

Matrix<int> a(2,2);


That is, we can refer to Matrix objects now with exactly the same notation as primitive 2-D arrays in C++: array[row][column]. Our operator overloading is faking it well enough to keep a consistent interface with analogous structures, but add much more functionality and safety. Isn't this cool?

The = operator works the same way as in Row. It sets the size of the current Matrix to that of the source, and then copies all of the objects to the current Matrix. Now we can do the following:

Matrix<__int64> m(1000,1000);
Matrix<__int64> n=m;

...and we have two very large matrices of 64-bit integers.
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-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials