【问题标题】:Question about () operator overloading关于 () 运算符重载的问题
【发布时间】:2011-08-01 18:36:25
【问题描述】:
    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 );

对于这一行: 布尔运算符(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

谢谢

【问题讨论】:

  • 很高兴从书籍中学习 C++。通常不会有很多错别字。

标签: c++ operator-overloading


【解决方案1】:

你明白了 - 这可能是一个错字,它应该是

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

【讨论】:

  • @Ben 是的,这是个好主意,但我只想解决他的问题。有时很难回答关于 SO 的问题,不想提供大量不相关的建议:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多