1

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:

  1. Can some one please suggest a good tutorial on operator overloading?
  2. what does Array & operator << (int x); mean?
  3. int operator [] (int i) - if this is a function why are we putting square brackets here?
  4. Array &Array::operator means what?
  5. what is *this?

Please help me...I am new to c++ so have these doubts.... Thanks in advance

Community
  • 1
  • 1
Frustrated Coder
  • 4,417
  • 10
  • 31
  • 34

7 Answers7

2
  1. Google is your friend :) or have a look at http://www.parashift.com/c++-faq-lite/operator-overloading.html
  2. It overloads the << operator
  3. It overloads the [] operator, so you can use yourobject[something]. It's used e.g. by std::map
  4. It looks like the beginning of the implementation of an overloaded operator.
  5. It's a reference to the current object.
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
1

Answer to (1): Did you look at the FAQ? http://www.parashift.com/c++-faq-lite/operator-overloading.html

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
1
  1. This.
  2. The operator << is being overloaded to take an integer as an argument, and it returns a reference to an Array class.
  3. The operator [] is being overloaded to accept an integer as an argument. It returns an integer.
  4. Returning a reference to Array, the operator << belonging to Array
  5. this is the pointer to the current object, so *this dereferences that pointer.

Perhaps you really need this link....

Ben Stott
  • 2,218
  • 17
  • 23
1

Everytime you encount T operatorX, where X can be any of (),[],<<,>>,+,+=,=,--,++ etc., that's what is called "operator overloading". It does just what it says -- it enables you to use that operator on the class it's overloaded for. For example your array could be accessed like this:

Array myArr(10);
myArr[0] == 5;
myArr[1] == 10;
//   ^^^ --- this is using the `operator[]` with an int parameter
Xeo
  • 129,499
  • 52
  • 291
  • 397
1

Here is good one. I used to reference it.

Operator-Overloading/Catalog0200_Operator-Overloading.htm">http://www.java2s.com/Tutorial/Cpp/0200_Operator-Overloading/Catalog0200_Operator-Overloading.htm

Sarfraz Ahmed
  • 1,349
  • 6
  • 23
  • 43
1

Adding to other answers,this is a nice source to learn operator overloading by examples :-

http://www.java2s.com/Tutorial/Cpp/0200__Operator-Overloading/Catalog0200__Operator-Overloading.htm

deovrat singh
  • 1,220
  • 2
  • 17
  • 33
0

I'm sorry can't help more on the first problem, but I refer to 'C++ primer' for some basic concept I'm not clear.

I think operator overloading does not have too much difference between method overloading except for the naming convention. If you know how to overload a method in C++, it's not very difficult to understand operator overloading once you get used to the syntax.

As other Object-Oriented language, in C++, if we want to overload a method, we just declare multiple versions of method to be overloaded with a shared method name and different parameter lists. Operator overloading looks the same in spite of we should know what is the name of the operator(which is equivalent to method name in method overloading). To name an operator, we should first use the keyword operator followed by the operator we wanna overload. Take your second question for example: here, we are going to overload the operator <<(the output operator), and we use operator << telling the compiler "That's what I want to overload." Also because an operator is just like a method, it should have a return value. In problem 2, Array & is returned, which is an reference to an Array type. The Array in "Array::opertor <<" means operator << is a member of class Array.

Hope this can give you some hint understanding this code snippet. You can find all the concepts in a definitive C++ tutorial.

Good luck.

Summer_More_More_Tea
  • 12,740
  • 12
  • 51
  • 83