Overload []. : Index « Overload « C++






Overload [].

Overload [].
 
#include <iostream>
using namespace std;

const int SIZE = 3;

class MyClass {
  int a[SIZE];
public:
  MyClass() {
    register int i;

    for(i = 0; i <SIZE; i++) 
       a[i] = i;
  }
  int operator[](int i) {
     return a[i];
     
  }
};

int main()
{
  MyClass myObject;

  cout << myObject[2];

  return 0;
}



           
         
  








Related examples in the same category

1.Operator overload: new, delete, new[] and delete[]Operator overload: new, delete, new[] and delete[]
2.Define operator []Define operator []
3.Return a reference from [].Return a reference from [].
4.String class: Index characters and '=' operatorString class: Index characters and '=' operator
5.Index operator for class