Unary operators
Bitwise not isn’t the only
operator that takes a single argument. Its companion, the logical not
(!), will take a
true value and produce a false
value. The
unary minus (-) and unary
plus (+) are the same
operators as binary minus and plus; the compiler figures out which usage is
intended by the way you write the expression. For instance, the
statement
x = -a;
has an obvious meaning. The compiler can
figure out:
x = a * -b;
but the reader might get confused, so it
is safer to say:
x = a * (-b);
The unary minus produces the negative of
the value. Unary plus provides symmetry with unary minus, although it
doesn’t actually do anything.
The increment and
decrement
operators (++ and --) were introduced earlier in this chapter.
These are the only operators other than those involving assignment that have
side effects. These operators increase or decrease the
variable by one unit, although “unit” can have different meanings
according to the data type – this is especially true with
pointers.
The
last unary operators are the address-of
(&), dereference
(* and ->), and
cast operators in C and C++, and
new and delete in
C++. Address-of and dereference are used with pointers,
described in this chapter. Casting is described later in this chapter, and
new and delete are introduced in Chapter
4.