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:



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

12 comments to 9.9 — Overloading the parenthesis operator

  • sergk

    This concept called “function objects” or “functors”.
    Functors in it’s essence is C++ style callbacks.

  • sergk


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



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

  • Xudong

    Here’s a noobish question:

    Shouldn’t it be


  • sandipa maheswari

    be more explanatory

  • bla



    which looks quite ugly…

  • sunil_s


    • 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.

  • Whew! It’s getting more complicated

  • Janez

    I don’t really get what is the "advantage" of overloading () operator, besides the name is shorter than if you would do the same thing with a member function. For other operators it was more logical but I don’t get this one (I understand how it works, but not why would someone use it).

Leave a Comment




five × two =
