关于()操作符重载的问题

Question about () operator overloading

本文关键字:问题 重载 操作符 关于      更新时间:2023-10-16
    class Message
{
    public:
        std::string getHeader (const std::string& header_name) const;
        // other methods...
};
class MessageSorter
{
    public:
        // take the field to sort by in the constructor
        MessageSorter (const std::string& field) : _field( field ) {}
        bool operator (const Message& lhs, const Message& rhs)
        {
            // get the field to sort by and make the comparison
            return lhs.getHeader( _field ) < rhs.getHeader( _field );
        }
    private:
        std::string _field;
};
std::vector<Messages> messages;
// read in messages
MessageSorter comparator;
sort( messages.begin(), messages.end(), comparator );

对于这一行:bool操作符(const Message&lhs, const Message&rhs)

对吗?应该是吗?bool operator() (const Message&lhs, const Message&rhs)

这段代码是Functor的教程示例代码。可以在这里看到:http://www.cprogramming.com/tutorial/functors-function-objects-in-c + + . html

谢谢

你明白了-可能是打错了,应该是

bool operator()(const Message& lhs, const Message& rhs)