How can it be useful to overload the “function cal

2019-01-11 03:26发布

I recently discovered that in C++ you can overload the "function call" operator, in a strange way in which you have to write two pair of parenthesis to do so:

class A { 
  int n;
public: 
  void operator ()() const; 
};

And then use it this way:

A a;
a();

When is this useful?

7条回答
甜甜的少女心
2楼-- · 2019-01-11 03:58

The compiler can also inline the functor and the function call. It cannot inline a function pointer, however. This way, using the function call operator can significantly improve performance when it is used for example with the standard C++ libary algorithms.

查看更多
登录 后发表回答