Cryptography Reference
In-Depth Information
parameter list of the operator function. Within the member function reference to
the elements of the implicit argument is made simply by naming them without
context. Furthermore, a reference to the implicit object can be made via the
special pointer this , as in the following implementation of the operator “ = ”:
const LINT& LINT::operator= (const LINT& ln)
{
if (ln.status == E_LINT_INV)
panic (E_LINT_VAL "=", 2 __LINE__);
First, a check is made as to whether the references to the right and left arguments
are identical, since in this case copying is unnecessary. Otherwise, the digits of
the numerical representation of ln are copied into those of the implied left argu-
ment *this , just as the value of status , and with *this the reference to the implicit
argument is returned.
if (&ln != this)
{
cpy_l (n_l, ln.n_l);
status = ln.status;
}
return *this;
}
One might ask whether the assignment operator must necessarily return any
valueatall,sinceafter LINT::operator =(const LINT&) is called the intended
assignment appears to have been accomplished. However, the answer to the
question is clear if one recalls that expressions of the form
f(a=b);
are allowed. According to the semantics of C++, such an expression would result
in a call to the function f with the result of the assignment a=b as argument.
Thus is it imperative that the assignment operator return the assigned value as
result, and for reasons of efficiency this is done by reference. A special case of
such an expression is
a=b=c;
where the assignment operator is called two times, one after the other. At the
second call the result of the first assignment b=c is assigned to a .
In contrast to the operator “ * ”, the operator “ *= ” changes the leftmost of the
two passed factors by overwriting it with the value of the product. The meaning
of the expression a*=b as an abbreviated form of a=a*b should, of course,
remain true for LINT objects. Therefore, the operator “ *= ” can, like the operator
Search WWH ::




Custom Search