operator overloading

A

ashu

i have studied this example of overloading [] operator, but i don`t
know exactly what happened in code .kindly explain me:

#include <iostream>
using namespace std;
const int SIZE = 3;
class atype {
int a[SIZE];
public:
atype() {
register int i;
for(i=0; i<SIZE; i++) a = i;
}
int &operator[](int i) {return a;}
};
int main()
{
atype ob;
cout << ob[2]; // displays 2
cout << " ";
ob[2] = 25; // [] on left of =
cout << ob[2]; // now displays 25
return 0;
}


..rest code is ok.what happened in this line of code and how
ob[2] = 25;
what it mean? is it mean ob.a[2]=25 ? kindly explain.
 
O

osmium

ashu said:
Ii have studied this example of overloading [] operator, but i don`t
know exactly what happened in code .kindly explain me:

#include <iostream>
using namespace std;
const int SIZE = 3;
class atype {
int a[SIZE];
public:
atype() {
register int i;
for(i=0; i<SIZE; i++) a = i;
}
int &operator[](int i) {return a;}


This has the same effect as
int& bracket(int)
{
return a;
}

where the word bracket replaces the glyphs for brackets in the original

};
int main()
{
atype ob;
cout << ob[2]; // displays 2
cout << " ";
ob[2] = 25; // [] on left of =
cout << ob[2]; // now displays 25
return 0;
}

You do realize, I hope, that the example is pointless, it is simply provided
to show you the syntax?. The constructor sets
a[k] = k for all elements in the array,
but I think you already knew that.
.rest code is ok.what happened in this line of code and how
ob[2] = 25;
what it mean? is it mean ob.a[2]=25 ? kindly explain.
 
O

osmium

ashu said:
may someone is kind enough to give me indepth knowledge of operator
overloading specially- [],(),comma opeartor,new and delete.

The hard copy of the Eckel book I have has syntax for, AFAIK, *all* the
operators that can be overloaded. The book is free on line, I dislike
E-books so I didn't confirm that all that excruciating detail is in the
E-book The examples are similar to what you posted - no obvious practical
usage.

Here is a somewhat more useful example of using the [] operator, it gives
you an array that has bounds checking, something lacking in C - but
available if you jump through the right hoops in C++. In a real program it
should "throw" an error instead of printing an error message.. Doing so
here would simply obscure the point of the code.

To use, remove the STOP macros where they appear in the body of the code -
they are there to humor my compiler.

/* 070517 demonstrate overloading of [] operator
Create an array of int with 10 elements and detect
out of range errors */

#include <iostream>

#define STOP while(1) cin.get();

using namespace std;

class Arr
{
public:
int& operator[] (int);
private:
enum{K=10};
int a[K];
};
//............................
int& Arr::eek:perator[] (int i)
{
if(i<0 || i>9)
{
cout << "Subscript out of range in class Arr\n"
<< "Valid range is 0..9 and value provided was "
<< i << endl;
STOP;
exit(1);
}
return a;
}
//==============
int main()
{
Arr arr;
for(int j=0; j<10; j++)
arr[j] = 100 + j;
for(int k=0; k<10; k++)
cout << arr[k] << endl;
// next two cause error messages
//cout << arr[-1] << endl;
//cout << arr[10] << endl;

STOP;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top