the code below will
calls a member function of the Sales_item object named item1. -C++ Primer 4th edi.(book)
// first check that item1 and item2 represent the same book
if (item1.same_isbn(item2))
which will return true if ISBN of item1 is equal to item2, but thee give some exercise which make me want to use opposite effect to the original condition, not equal(obviously), so I put it like this
if (!item1.same_isbn(item2))
the compiler will compile but the result miss from the expected one, so there something that told me that where code is valid, but processing something that I’m not expected.
so, per title said, does the not(“!”) operator actually work the same everywhere?
The
!unary operator can be applied to any scalar (numeric or pointer) expression. The operand is converted tobool, and the result is true if the expression is false, and false if it’s true.For a numeric operand,
!xis equivalent tox != 0. It’s the same for a pointer operand (it’s true if the pointer is a null pointer.)It can also be applied to an expression of any type for which there’s a conversion to
bool, or, of course, for any type for which an overloadedoperator !is defined.