|
What Operators can I Overload?
The C++ language has a large number of operators.
C++ allows you to overload the following list:
- Assignment operator (=)
- Arithmetic operators (+, -, *, /, %)
- Increment/Decrement operators (++, --)
- Operator-assignment (+=, -=, *=, /=, <<=, &=, |= etc.)
- Unary operators (-, +, !, ~ etc.)
- Relational (Comparision) operators (<, >, <=, ==, != etc.)
- Bitshift/Extraction operators (<<, >>)
- Subscript operator ([ ])
- Function call '()' operator
- Bit operators (&, |, ^, ~)
- Logical operators (&&, ||, !)
- Comma (,) operator
- Pointer to member (->)
- new and delete operators.
You CANNOT overload the following operators:
- Ternary expression operator (? :)
- Scope operator (::)
- Class member operator (.)
- Preprocessor symbol operator (# and ##)
- Pointer to member operator (.*)
- sizeof operator
Since the number of C++ operators is very large, it is easier to memorize the operators
that cannot be overloaded and assume that anything else can be overloaded.
|
Overloading Rules
The following rules must be noted:
- You cannot invent new operators that do not exist in the language. For instance, @ is not a valid C++ operator
and cannot be overloaded.
- Overloaded operators must follow the same syntax rules. For example:
SomeClass x, y;
x ++ 12; // Not allowed: ++ is not a binary operator.
x++; // This is allowed
% x; // Not allowed: % is not an unary operator.
y = x % 3; // This is allowed
- You cannot change the way an operator works between native C++ types. For instance, you cannot change how + works
between a double and an integer variable, as this operation is native to the compiler. You can however change how +
works between a Complex class and a double variable, since Complex isn't a native C++ type.
- The order of operator evaluation remains unchanged. You cannot change the operator precedence rules.
- You cannot change the operator associativity rules either. For example, the unary minus (-)
operator associates from right to left.
X = -Y; // Unary minus associates from right to left.
X = Y-; // You CANNOT alter unary minus to go from left to right.
- Overloading some operators doesn't implictly define other operators for you. For example,
overloading the + and = operators for a class doesn't mean that the += operator is overloaded
automatically for you. You will need to write separate overload code for the += operator
as well.
Unwritten Rule: Try to overload operators where they make sense. For example,
virtually everyone is used to seeing arithmetic symbols for matrix operations such as addition,
subtraction, multiplication etc., because this is how we were taught to represent them at
school (surely you weren't sleeping there, were you?). Therefore, it makes sense to overload the
arithmetic operators (+, -, *) for matrix objects. You should note that just because
you CAN overload the + sign to mean subtraction between two matrices, doesn't mean you
SHOULD actually do it. You should keep the overloaded operators analogous to their
original meaning. Call this the "behaves like an int" rule -- i.e. if an operator behaves
a certain way for an int, you should overload it to an analogous meaning for your class.
|
|