Overloading of [] and ()
Let us see some unusual operators to overload.
Let us say you have an array class of some type and you want to access the elements of the array using familiar [] operator, then you can overload this.
So here comes code
The class looks quite straight forward. It is array of ints and subscript operator returns the element of the array. So the function can be
But there is still some problem in this code. Don't you think so? Let us look at its usage
obj[1] = 22 needs us to be able to place our operator function on LHS. Which means --- we need a reference return type.
So the proper class with operator functions is
Now you should ask me why are there two subscript functions. The reason is, if there is a constant object, it can call only const functions. const functions should not modify the state of the object. That is the reason one [] operator function is const and returns const int &
In this case funobj is function object and is also called a functor.
Subscript operator overloading
Let us say you have an array class of some type and you want to access the elements of the array using familiar [] operator, then you can overload this.
So here comes code
class Array
{
int *numarray;
int size;
public:
--------
int operator [](int index);
-----
};
The class looks quite straight forward. It is array of ints and subscript operator returns the element of the array. So the function can be
int Array::operator[](int index)
{
return numarray[index];
}
But there is still some problem in this code. Don't you think so? Let us look at its usage
Array obj(5); cout<<obj[0]; obj[1] = 22;
obj[1] = 22 needs us to be able to place our operator function on LHS. Which means --- we need a reference return type.
So the proper class with operator functions is
#include<iostream> using std::cout; class Array { int *element; int size; public: Array(int n=1); int & operator [](int index); const int &operator[](int index) const; ~Array(); Array(const Array & obj); Array& operator =(const Array &other); }; Array::Array(int n) { size = n; element = new int[n]; for(int i=0;i<n;i++) { element[i]=0; } } Array::Array(const Array &obj) { size = obj.size; element = new int[size]; for(int i=0;i<size;i++) { element[i]=obj.element[i]; } } Array& Array::operator = (const Array &other) { if(this!=&other){ delete []element; size = other.size; element = new int[size]; for(int i=0;i<size;i++) { element[i]=other.element[i]; } } return *this; } int &Array ::operator[](int n) { if(n>=0 && n<size) return element[n]; else return 0; } const int& Array::operator[](int n) const { if(n>=0 && n<size) return element[n]; } Array::~Array() { delete[]element; }
Now you should ask me why are there two subscript functions. The reason is, if there is a constant object, it can call only const functions. const functions should not modify the state of the object. That is the reason one [] operator function is const and returns const int &
Function call operator
Function call operator should be a member function. It is normally used for function objects such as iterators etc.
Let us look at an example.
class Cube
{
public:
int operator ()(int num);
};
int Cube::operator()(int num)
{
return num*num*num;
}
As we can see the class has an operator function which takes an int and returns its cube. Let us see its usage.
Cube funobj;
cout<<funobj(4);
In this case funobj is function object and is also called a functor.
Comments
Post a Comment