In general operator() is used to give an object function calling semantics. If your object can be seen as "doing something" then it might make sense to use this operator. These objects are known as functors. They are often used to encapsulate some action that will be done repeatedly especially on many objects.
See the STL headers algorithm
and functional
for interesting examples and uses.
By the way, you are allocating an A but assigning it to an object instead of a pointer. And you are calling your A::operator()
when you are allocating A, and your complier should complain. Change A a = new A()
to something like A* a = new A;
or simpler A a;
since a
doesn't need to exist beyond main's execution. If you use the pointer version you have to call operator()
like so (*a)(5,4);
Also your A::operator()
will have no effect. The x
and y
arguments are passed by value. Pass them by reference instead.