one place a reference is needed and pointers will not work is overloading the [] operator. You couldn't implement this
as neatly using pointers.
And the reference can be used on both left and right side of the operators, as illustrated in main() below.
#include <iostream>
using namespace std;
class matrix
{
private:
int array[10];
public:
matrix() {memset(array,0,sizeof(array));}
int& operator[](int index) {return array[index];}
};
int main()
{
matrix m;
m[0] = 1;
m[1] = 2;
int x = m[2];
cout << m[0] << endl;
cout << m[1] << endl;
return 0;
}
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Offline 21,950 posts
since Aug 2005