468,247 Members | 1,276 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,247 developers. It's quick & easy.

operator overloading

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] = i;
}
int &operator[](int i) {return a[i];}
};
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.

May 17 '07 #1
5 4605
"ashu" writes:
>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] = i;
}
int &operator[](int i) {return a[i];}
This has the same effect as
int& bracket(int)
{
return a[i];
}

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.

May 18 '07 #2
"osmium" wrote:
>This has the same effect as
int& bracket(int)
Oops! should be

int& bracket(int i)
{
return a[i];
}

May 18 '07 #3
On May 17, 4:29 pm, "osmium" <r124c4u...@comcast.netwrote:
"osmium" wrote:
This has the same effect as
int& bracket(int)

Oops! should be

int& bracket(int i)
{
return a[i];
}
may someone is kind enough to give me indepth knowledge of operator
overloading specially- [],(),comma opeartor,new and delete.

May 18 '07 #4
ashu wrote:
On May 17, 4:29 pm, "osmium" <r124c4u...@comcast.netwrote:
>"osmium" wrote:
>>This has the same effect as
int& bracket(int)
Oops! should be

int& bracket(int i)
>> {
return a[i];
}

may someone is kind enough to give me indepth knowledge of operator
overloading specially- [],(),comma opeartor,new and delete.
Read Stroustrup chapter 11.

--
Ian Collins.
May 18 '07 #5
"ashu" writes:
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::operator[] (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[i];
}
//==============
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;
}
May 18 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Edward Diener | last post: by
2 posts views Thread by pmatos | last post: by
67 posts views Thread by carlos | last post: by
5 posts views Thread by Jerry Fleming | last post: by
3 posts views Thread by y-man | last post: by
9 posts views Thread by sturlamolden | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.