|
This question should be used to assess the skills of a developer
with 1-2 years experience using C++. All such developers should
be able to answer this question correctly.
What
is operator overloading and when is it used?
Operator overloading
allows a programmer to provide a more convenient and concise
syntax by re-defining the meaning of an operator relative
to a given class than would be provided by conventional functions.
Judicious use of operator overloading makes code easier to
write, read, understand and maintain.
The precedent for
a shorthand notation already exists in the language grammar
for built-in types:
X=a+b*c;
is more readable than:
AssignTo(&X,SumOf(a,ProductOf(b,c)));
Operator overloading
allows the programmer to do for user-defined types what the
grammar does for built-in types.
Syntax
Defining an overloaded
operator is exactly the same as defining a function where
the function name begins with the word operator and
ends with the operator symbol itself. This allows the compiler
to substitute the function call when it finds the operator
used with the types specified as arguments. For example:
class
complex {
…
public:
const
complex operator+(const complex& operand);
complex&
operator=(const complex& operand);
…
};
Given the complex
class above, and three complex types (a, b, and c) these two
lines of code are identical:
c
= a+b;
c.operator=(a.operator+(b));
Built-In Types:
The candidate may
fail to mention that operators cannot be overloaded for built-in
types. This may indicate that he/she understands the mechanics
but not the intent. The intent of operator overloading is
to make code more readable and maintainable. If you could
overload operators for built-in types, 1+2 may not necessarily
equal 3. This would not contribute to maintainability.
Be sure to ask
the candidate why operators may not be overloaded for built-in
types.
Members,
Friends, and Nonmembers
The less experienced
candidate will probably confine his/her discussion to member
operators. The more experienced candidate may mention that
some operators must be member functions. These are: operator=,
operator(), operator[] and operator->. All other operators
could be members, nonmembers or friends. As a general rule,
the programmer should prefer member operators over friends
or nonmembers because these operators are intimately associated
with the class.
The main exception
is when the left and right hand operands are of different
classes. Returning to the complex example with an additional
operator+ overload:
class
complex {
…
public:
const
complex operator+(const complex& operand);
const
complex operator+(int operand);
complex&
operator=(const complex& operand);
…
};
Now, given the
complex types c and a, this is legal:
c=a+1;
but this is not:
c=1+a;
For the latter
to work, the programmer would have to be able to overload
the + operator for type int, which is illegal because it would
cause more problems than it solves. A friend function is called
for:
friend
complex operator+(
const
complex& left,
const
complex& right
);
If the candidate
doesn't point out that this friend function has two
arguments, while the member function version has only one,
ask. The friend function, not being a member of complex, has
no this pointer, and thus needs both the left and right
operands as arguments.
Summary:
Operator overloading
is not really needed - it just makes code more readable and
maintainable. Like any tool, if overused, or used inappropriately,
it can harm the user
About
the author
Tom Jackson has over 16 years of software development experience
on a large variety of projects from shrink-wrapped Client/Server
applications to games. Tom has lead teams of software developers
small and large. Throughout his career, Tom has interviewed
full-time, part-time, consultants, contractors, and telecommuters
for a variety of technical positions. "To have a successful
interview," he says, "you need to do as much (or more) to
prepare for the interview as the candidate does."
|