Possible Duplicate:
Operator overloading
Hi guys can some one please suggest a good tutorial on operator overloading?
I was going through this code on operator overloading and I have the following doubts
code:
#include <iostream>
using namespace std;
class Array {
int *a;
int size;
int capacity;
public:
Array (int c) {a=new int[c]; size=0; capacity =c; } ;
Array & operator << (int x);
int operator [] (int i) {
if (i<size) return a[i] ;
else {
cout <<"Segmentation Fault Prevented!\n";
return -1;
}
};
};
Array &Array::operator << (int x) {
if (size < capacity) a[size++] = x;
else {
int *tmp = new int[size+capacity];
for (int j=0; j<size; j++)
tmp[j]=a[j];
delete [] a;
a = tmp;
a[size++]=x;
capacity=size+capacity;
}
return *this;
} ;
int main (int agrc, char *argv[] ) {
Array b(10);
for (int i=0; i<100; i++) b << i;
b << 1 << 2 << 3;
for (int i=0; i<105; i++) cout << b[i] << endl;
}
I have these doubts:
- Can some one please suggest a good tutorial on operator overloading?
- what does
Array & operator << (int x);
mean? int operator [] (int i)
- if this is a function why are we putting square brackets here?Array &Array::operator
means what?- what is
*this
?
Please help me...I am new to c++ so have these doubts.... Thanks in advance