Facebook Page Twitter Page LinkedIn Page
× C++ Tutorial C++ Advanced C++ Resources


Function Call operator ( ), like the subscript operator, is considered a binary operator. function call operator can be overloaded for class objects and the declaration is identical to any binary operator. Overloading of (), does not modify how functions are called; rather, it modifies how the operator is to be interpreted when applied to class objects.

Example: Overloading Function Call Operator

In the example below, function call () operator is overloaded to assign the private data members of the Vector class object.

#include <iostream>
using namespace std;
 
class Vector {
  private:
    int x;
    int y;
  public:
    //function to display vector
    void displayVector() {
      cout<<"("<<x<<", "<<y<<")\n"; 
    } 
    //function for overloading ()
    Vector operator() (int a, int b) {
      x = a;
      y = b;
      return *this;
    }       
};
int main () {
  Vector v1, v2;
  
  //objects with function call
  v1(10, 15);
  v2(5, 25);

  v1.displayVector();
  v2.displayVector();  
  return 0;
}

The output of the above code will be:

(10, 15)
(5, 25)

❮ C++ - Operator Overloading

5