Browsing through some of the open source code implementation in C++, came across the function void operator()()

Q. Why would one use operator() in the first place?

A. Simplicity of usage. Of course, less intuitive if you are seeing it's use for the very first time.

The program below implements a matrix and uses operator() as a replacement of subscript operator[] to retrieve and set values at the particular location.

#include <iostream> //File: Matrix.h class Matrix { public: Matrix(size_t rows, size_t cols); Matrix(const Matrix &m); virtual ~Matrix(); Matrix& operator=(const Matrix &m); double& operator()(size_t row, size_t col); //double operator()(size_t row, size_t col) const; size_t getRows() const; size_t getCols() const; private: Matrix(); void copy(const Matrix &m); void init(double value); size_t _nrows, _ncols, _size; double *_data; };

Excercise

Why is the method commented out in the above code?

//double operator()(size_t row, size_t col) const;

Read more ...