
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
122
|
Chapter 3: Classes and Structures
The || operator works the same as the && operator, except that the initial test is done
using the
true operator rather than the false operator.
The
?: operator requires the overloading of the true operator to be indirectly over-
loaded. Note that this, in turn, requires the overloading of the
false operator for
symmetry. The
?: operator takes a conditional expression as input and evaluates
either its
true or false expression. This operator can be defined as follows:
conditional-expression ? true-expression : false-expression
The ?: operator invokes the true operator to determine which expression of this
operator should be evaluated. Note that if an implicit conversion to
bool exists, it
will be used in preference to the true operator.
When implementing these operators, you should first check to determine whether
any parameters in the overloaded operator methods were set to
null. The code for
the overloaded
& operator has been modified to do this:
public static ObjState operator &(ObjState obj1, ObjState obj2)
{
if (obj1 == null || obj2 == null)
{
throw (new ArgumentNullException("Neither object may be null."));
}
if (obj1.state >= 0 && obj2.state >= 0)
return (new ObjState(1));
else
return (new ObjState(-1));
}
See Also
See the “Operator Overloading Usage ...