473,385 Members | 2,003 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,385 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 5338
"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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.