C++/Overloading 2021. 3. 16. 00:27

괄호 연산자 오버로딩 (Parenthesis Operator Overloading)

  • ()(parenthesis)

  • Functor

    • 객체를 함수처럼 사용하는 것

예제

  #include <iostream>

  class Accumulator
  {
    int counter_ = 0;

  public:
    int operator() (int i) { return (counter_ += i); }
  };

  int        main()
  {
    using namespace std;

    Accumulator acc;

    cout << acc(10) << endl;
    cout << acc(20) << endl;
  }

  /* stdout
  10
  30
  */