N
N
Nikita Gusakov2013-03-29 00:36:55
Programming
Nikita Gusakov, 2013-03-29 00:36:55

Overloading logical operators?

Tell me, is it possible to implicitly overload all logical operators?

class Product{
private:
    int price;
    string name;
public:
    friend bool operator >(Product a, Product b){
        return a.price > b.price;
    }
};

In the code, the overload of the “greater than” operator, but how to overload the rest without explicitly prescribing?
PS, that is, it is obvious that for equality comparison there will be the following code:
friend bool operator ==(Product a, Product b){
        return a.price == b.price;
    }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Door, 2013-03-29
@hell0w0rd

you can only define 2 operators < and == , and use namespace std::rel_opswhich has definitions of operators != , > , <= , >= . It?

P
Paul, 2013-03-29
@Paul

Take a look at boost/operators.hpp, by the way, maybe this is what you need? It's like std::rel_ops, only much more powerful.

A
Andrey Burov, 2013-03-29
@BuriK666

Do you want to use the same code for different operators?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question