dimanche 8 septembre 2019

Logical AND overloading in C++

Here's a class that implements the && operator overloading.

template <typename T>
struct Specification {
    virtual bool is_satisfied(T* item) = 0;

    AndSpecification<T> operator&&(Specification<T> &&other) {
        return AndSpecification<T>(*this, other);
    }
};

That's the so called combinator from the Specification pattern. What I don't get is why we have to use double && in the list of parameters AndSpecification<T> operator&&(Specification<T> &&other).

I understand this line should be read as follows:

  • AndSpecification<T> - is the type we get after multiplying specification via the && operator.

  • operator&& - the operator to overload

  • (Specification<T> &&other) - Specification<T> is the type of the other parameter. The parameter is taken by reference so we're using &.

But why use the second ampersand here?

Aucun commentaire:

Enregistrer un commentaire