Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question about () operator overloading

    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 );

For this line: bool operator (const Message& lhs, const Message& rhs)

is this right? Should it be bool operator() (const Message& lhs, const Message& rhs)

This code is a tutorial exmample code for Functor. can be seen here: http://www.cprogramming.com/tutorial/functors-function-objects-in-c++.html

Thank you

like image 838
David Degea Avatar asked Dec 12 '25 15:12

David Degea


1 Answers

You got it - It's probably a typo, it should read

bool operator()(const Message& lhs, const Message& rhs)
like image 134
Josh Avatar answered Dec 15 '25 04:12

Josh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!