객체가 함수처럼 동작한다고 하여 함수 객체라고 한다.
함수 호출 연산자 :
() 연산자는 "함수 호출 연산자" 라고 불린다.
해당 연산자를 overloading 하여 함수 객체를 구현할 수 있다.
예제 :
class Functor
{
public:
void operator()()
{
std::cout << value << std::endl;
}
void operator()(int num)
{
value += num;
std::cout << value << std::endl;
}
public:
int value = 10;
};
int main()
{
Functor functor;
functor(); // 10
functor(1); // 11
return 0;
}
사용 이유 :
일반적인 함수와 달리 함수 객체를 사용하면 속성을 지닐 수 있는 것이 가능하며, 일반적인 함수보다 빠르다.
'DirectX11 > Rookiss' 카테고리의 다른 글
콜백 함수 (0) | 2024.03.16 |
---|---|
함수 포인터 (0) | 2024.03.16 |
비트 연산 (0) | 2024.03.16 |
Modern C++_2 (0) | 2024.03.16 |
Mordern C++ (2) | 2024.03.15 |