问题(20210929-02):C++结构体函数调用运算符 () 重载

代码

/*******************************************************************
 *   > File Name: struct.cpp
 *   > Create Time: 2021年09月30日 星期四 04时25分43秒
 ******************************************************************/

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{

    struct CompareEvents
    {
        bool operator()(const int ev1, const int ev2) const
        {
            bool retVal = ev1 < ev2;
            if (ev1 == ev2)
            {
                //retVal = (ev1 > ev2 );
                cout << "ev1 == ev2" << endl;
            }
            cout << "operator---" << endl;

            return retVal;
        }
    };
    
    CompareEvents a;
    bool b;
    b = a(100, 100);
    cout << "b = " << b << endl;


    return 0;
}

编译/运行

lanfeiy@virtual-machine:~/workSpace/cpp_test$ make
g++ -o struct struct.cpp -g -Wall -std=c++11
lanfeiy@msil-virtual-machine:~/workSpace/cpp_test$ ./struct
ev1 == ev2
operator---
b = 0