[ fromfile: functions.xml id: operatoroverloading ]
The keyword operator
is used in C++ to define a new meaning for an operator symbol such as +
, -
, =
, *
, &
, etc.
Adding a new meaning to an operator symbol is a specialized form of overloading.
Example 5.6. src/complex/complex.h
#include <iostream> using namespace std; class Complex { // binary nonmember friend function declarations friend ostream& operator<<(ostream& out, const Complex& c); friend Complex operator-(const Complex& c1, const Complex & c2); friend Complex operator*(const Complex& c1, const Complex & c2); friend Complex operator/(const Complex& c1, const Complex & c2); public: Complex(double re = 0.0, double im = 0.0); // binary member function operators Complex& operator+= (const Complex& c); Complex& operator-= (const Complex& c); Complex operator+(const Complex & c2); private: double m_Re, m_Im; };
The operators declared in Example 5.6 are all binary (accepting 2 operands).
For the member functions, there is only one formal parameter because the first (left) operand is implicit: *this
.
The definitions of the member operators are shown in Example 5.7.
Example 5.7. src/complex/complex.cpp
[ . . . . ] Complex& Complex::operator+=(const Complex& c) { m_Re += c.m_Re; m_Im += c.m_Im; return *this; } Complex Complex::operator+(const Complex& c2) { return Complex(m_Re + c2.m_Re, m_Im + c2.m_Im); } Complex& Complex::operator-=(const Complex& c) { m_Re -= c.m_Re; m_Im -= c.m_Im; return *this; }
Example 5.8 shows the definitions of the nonmember friend functions.
They are defined like ordinary global functions.
Example 5.8. src/complex/complex.cpp
[ . . . . ] ostream& operator<<(ostream& out, const Complex& c) { out << '(' << c.m_Re << ',' << c.m_Im << ')' ; return out; } Complex operator-(const Complex& c1, const Complex& c2) { return Complex(c1.m_Re - c2.m_Re, c1.m_Im - c2.m_Im); }
Example 5.9. src/complex/complex-test.cpp
#include "complex.h" #include <iostream> int main() { using namespace std; Complex c1(3.4, 5.6); Complex c2(7.8, 1.2); cout << c1 << " + " << c2 << " = " << c1 + c2 << endl; cout << c1 << " - " << c2 << " = " << c1 - c2 << endl; Complex c3 = c1 * c2; cout << c1 << " * " << c2 << " = " << c3 << endl; cout << c3 << " / " << c2 << " = " << c3 / c2 << endl; cout << c3 << " / " << c1 << " = " << c3 / c1 << endl; return 0; }
(3.4,5.6) + (7.8,1.2) = (11.2,6.8) (3.4,5.6) - (7.8,1.2) = (-4.4,4.4) (3.4,5.6) * (7.8,1.2) = (19.8,47.76) (19.8,47.76) / (7.8,1.2) = (3.4,5.6) (19.8,47.76) / (3.4,5.6) = (7.8,1.2)
Only built-in operators can be overloaded.
It is possible to overload all the built-in binary and unary operators except for these:
The ternary conditional operator testExpr ? valueIfTrue : valueIfFalse
The scope resolution operator ::
Member select operators .
and .*
Tip | |
---|---|
Here is a way to remember which operators can be overloaded. If the symbol has a dot |
Note | |
---|---|
Overloading the comma operator is permitted but not recommended until you are a C++ expert. |
You can find a complete table of operator symbols and their characteristics in Section 19.1.
Note | |
---|---|
It is possible to define a new meaning for a built-in operator symbol so that it can be used with operands of different types. But it is not possible to change the associativity or the precedence of a built-in operator symbol. |
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |