函数调用运算符()也可以重载
    由于重载后使用的方式非常像函数的调用,因此称为仿函数
    仿函数没有固定写法,非常灵活

    1. //函数调用运算符重载
    2. class MyPrint
    3. {
    4. public:
    5. //重载函数调用运算符
    6. void operator()(string test)
    7. {
    8. cout << test << endl;
    9. }
    10. };
    11. void Myprint2(string test)
    12. {
    13. cout << test << endl;
    14. }
    15. void test01()
    16. {
    17. MyPrint myprint;
    18. myprint("hello world"); //由于使用起来非常类似于函数调用,因此称为仿函数
    19. Myprint2("hello world!@!"); //普通函数调用
    20. }
    21. //仿函数非常灵活,没有固定的写法
    22. //加法类
    23. class MyAdd
    24. {
    25. public:
    26. int operator()(int num1, int num2)
    27. {
    28. return num1 + num2;
    29. }
    30. };
    31. void test02()
    32. {
    33. MyAdd myadd;
    34. int ret = myadd(10, 10);
    35. cout << "ret = " << ret << endl;
    36. //匿名函数对象
    37. cout << MyAdd()(100, 100) << endl;
    38. }
    39. int main()
    40. {
    41. test01();
    42. test02();
    43. system("pause");
    44. return 0;
    45. }