Operator Overloading  «Prev 

Operator Overloading Guidelines in C++

Any time a class uses new to construct objects, it should provide an explicitly overloaded assignment operator. This advice is analogous to our rule that such a class provide an explicit copy constructor.
The compiler-provided default assignment operator semantics would, in most cases, give spurious behavior. This leads to a suggested normal form for classes with heap-managed memory. This normal form rule applies as well to reference counted classes. The reason the assignment operator returns a reference is to allow assignment to work efficiently. This requires lvalue semantics.

In C++ you can give special meanings to operators, when they are used with user-defined classes. This is called operator overloading. You can implement C++ operator overloads by providing special member-functions on your classes that follow a particular naming convention. For example, to overload the + operator for your class, you would provide a member-function named operator+ on your class.
The following set of operators is commonly overloaded for user-defined classes:

  1. = (assignment operator)
  2. + - * (binary arithmetic operators)
  3. += -= *= (compound assignment operators)
  4. == != (comparison operators)