One useful application of operator() is array subscripting for more than one index, for example in matrices.
If I have a vector class, I can overload [] and everything is fine:
Code:
template<int n, typename T=double>
class Vector
{
...
T& operator[] (size_t index)
{
return data[index];
}
...
private:
T data[n];
};
If I create a matrix class, I cannot use the []-operator (unless I return a vector from the matrix) but I can use the ()-operator
Code:
template<int n, int m, typename T=double>
class Matrix
{
...
T& operator()(size_t, i, size_t j)
{
return data[i][j];
}
...
private:
T data[i][j];
};
That's one of the uses. Another is for function objects, which are used frequently in the STL, like sorting predicates etc.
I will post an example of that soon.