9.9 — Overloading the parenthesis operator

All of the overloaded operators you have seen so far let you define the type of the operator’s parameters, but the number of parameters is fixed based on the type of the operator. For example, the == operator always takes two parameters, whereas the logical NOT operator always takes one. The parenthesis operator (()) is a particularly interesting operator in that it allows you to vary both the type AND number of parameters it takes!

There are two things to keep in mind: first, the parenthesis operator must be implemented as a member function. Second, in non-class C++, the () operator is used to call functions or write subexpressions that evaluate with higher precedence. In the case of operator overloading, the () operator does neither -- rather, it is just a normal operator that calls a function (named operator()) like any other overloaded operator.

Let’s take a look at a common example that lends itself to overloading this operator:

Matrices are a key component of linear algebra, and are often used to do geometric modeling and 3d computer graphics work. In this case, all you need to recognize is that the Matrix class is a 4 by 4 two-dimensional array of doubles.

In the lesson on overloading the subscript operator, you learned that we could overload operator[] to provide direct access to a private one-dimensional array. However, in this case, we want access to a private two-dimensional array. Because operator[] only has one parameter, it is not sufficient to let us index a two-dimensional array.

However, because the () operator can take as many parameters as we want it to have, we can declare a version of operator() that takes two integers and use it to access our two-dimensional array. Here is an example of this:

Now we can declare a Matrix and access it’s elements like this:

which produces the result:


Now, let’s overload the () operator again, this time in a way that takes no parameters at all:

And here’s our new example:

which produces the result:


Because the () operator is so flexible, it can be tempting to use it for many different purposes. However, this is strongly discouraged, since the () symbol does not really give any indication of what the operator is doing. In our example above, it would be better to have written the erase functionality as a function called Clear() or Erase(), as cMatrix.Erase() is easier to understand than cMatrix() (which could do anything!).

Operator () is commonly overloaded with two parameters to index multidimensional arrays, or to retrieve a subset of a one dimensional array (returning all the elements from parameter 1 to parameter 2). Anything else is probably better written as a member function with a more descriptive name.

9.10 -- Overloading typecasts
9.8 -- Overloading the subscript operator

11 comments to 9.9 — Overloading the parenthesis operator

  • Whew! It's getting more complicated

  • sunil_s

    In above example cMatrix is a class object and you have not overloaded << operator. still "std::cout << cMatrix(1, 2)" statement is working fine without error. how is it possible??

    • dismas256

      that is possible because the parenthesis operator in that class returns a reference to a built-in datatype "double" which of course has its own overloaded stream extraction operator.

  • bla

    Is there a way to make the overloading of the parenthesis operator work usefully, when dealing with dynamically allocated objects?
    I did the overloading very similar to the way described here and the only way (that I found) to access my object is:


    which looks quite ugly...

  • sandipa maheswari

    be more explanatory

  • Xudong

    Here's a noobish question:

    double& Matrix::operator()(const int nCol, const int nRow)

    Shouldn't it be

    double& Matrix::operator()(const int &nCol, const int &nRow)


  • Wikipedia has some more information about functors here, for anybody interested in learning more about them.

    As I understand it, the basic idea of functors is that instead of passing a function pointer to a function to do a specific job, you pass a class with an overloaded () operator to do the same job. The advantage here is that classes can store information about the state of things, whereas functions can not.

    Overloading () for doing functors is an advanced-level C++ concept, so don't sweat it if you're just learning the language. :)

  • sergk

    I must say that functor callbacks is not perfect solution (actually, there is no such thing at all).

    For caller implementation one have to use either known interface - a base class with virtual operator(), or use template.

  • sergk

    This concept called "function objects" or "functors".
    Functors in it's essence is C++ style callbacks.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">