473,320 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 5326
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
67
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used...
3
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.