Jump to

v03-l2-operatorUse

Let us consider a class that defines the negation operator, for instance :

class Object {
public:
    bool operator!() {
        return false;
    }
};

Which of the following ways to use this operator is/are correct, if a is an instance of Object ?

A : a.operator!()

B : operator!(a)

C : not a

D : !a

???

Since the operator is only internally overloaded (with an object method), then the B approach is not possible. However, if the negation operator is externally overloaded (with a function defined outside of the class), for instance like this :

bool operator!(const Object& a) {
    return false;
}

then using operator!(a) is possible.