#include <iostream>
using namespace std;
class Fun
{
public:
int my_add(int x,int y)
{
return x+y;
}
//重載()
//第一個()是重載的符號 第二個()是標明要傳參
int operator()(int x,int y)
{
return x+y;
}
};
void test01()
{
Fun fun;
cout<<fun.my_add(100,200)<<endl;
cout<<fun.operator ()(100,200)<<endl;
//優化 fun和()結合 就會自動尋找()運算符
cout<<fun(100,200)<<endl;
//此處 fun(100,200)不是一個真正的函數 僅僅是一個對象名和()結合 調用()重載運算符而已
//fun不是函數名 只是fun(100,200)類似一個函數調用 所以將fun(100,200)叫做仿函數
//此處的Fun是類名稱
//Fun()匿名對象 Fun()(100,200) 就是匿名對象(100,200)
cout<<Fun()(100,200)<<endl;//瞭解
}
int main(int argc, char *argv[])
{
test01();
return 0;
}
運算結果: