Thinking in C++ Notes (12)

Chapter 12: Operator Overloading

  • All the operator used in expressions that contain only built-in data types cannot be overloaded, operators can only be overloaded when a expression contains user-defined types
  • when define a overloaded operator function, the number of arguments depends on :
    • whether it is a unary operator or a binary operator
    • whether it is a global function or a member function
    • Generally:
      unary operator , global function: 1 argument
      unary operator , member function: 0 argument
      binary operator, global function: 2 arguments
      binary operator, member function: 1 argument
    • the postfix version of ++ and -- need a dummy argument
  • you cannot:
    • combine operators that currently have no meaning in C
    • change the evaluation precedence of operators
    • change the number of arguments required by an operator

Arguments & return value

  • if you only need to read from the argument and not change it, pass it as a const reference( if the operator is a member function, make it a const member function)
  • the type of the return value depends on the expected meaning of the operator
  • the return value for all the assignment operators should be a non-const reference to the lvalue.
  • return value optimization
  • operator [] must be a member function with a single argument from which often returns a reference
  • operator -> must be a member function and it must return an object that also has a pointer dereference operator or it must return a pointer that can be used to select what the pointer dereference operator arrow is pointing at
  • operator ->*

Non-member operators

  • if the left-hand operand is an object of some other class, and the class you considered is the right-hand operand, you should overload the operator as an global function

    Operator                        Recommended use

    All unary operators            member
    =  ()  []  ->  ->*              must be member

    +=  -=  /=  *=  ^=
    &=  |=  %=  >>=              member

    all other binary operators   non-member

Overloading assignment =

  • '=' must be overloaded as a member function, otherwise you might attempt to redefine the built-in '=' sign which may cause problems

    MyType b;
    MyType a=b; // a has not been initialized so copy-constructor is called
    a=b; // a has been initialized so operator= is used
    MyType c(b); // it is better to use the explicit constructor form when initialize an object

  • when you are assigning two objects of the same type, you should always check first for self-assignment

Pointers in class

  • when your class contains pointers, you will always need to define four functions :
    • ordinary constructors
    • copy-constructor
    • operator=
    • destructor
  • Reference counting and copy-on-write
  • Compiler will create a type::operator=(type) automatically if you don't make one
    Memberwise assignment: if the class contains objects, the operator= for those objects is called recursively
  • assignment is forbidden when defined as a private function

Automatic type conversion

  • Constructor Conversion
    • with a constructor taking a single argument of another type, the compiler can perform an automatic type conversion
    • you can turn off the automatic type conversion by prefacing the constructor with "explicit"
  • Operator Conversion
    • define: operator keyword followed by the type you want to convert to
  • to prevent the ambiguity error, you should provide only a single automatic conversion from one type to another.
  • copying vs  initialization example on page 541

Last modified on 2007-06-22