Chapter 14Overloading C++ Operators

  • Explaining operator overloading
  • Rationale for overloading operators
  • Limitations, caveats, and choices in operator overloading
  • Summary of operators you can, cannot, and should not overload
  • How to overload unary plus, unary minus, increment, and decrement
  • How to overload the I/O streams operators (operator<< and operator>>)
  • How to overload the subscripting (array index) operator
  • How to overload the function call operator
  • How to overload the dereferencing operators (* and ->)
  • How to write conversion operators
  • How to overload the memory allocation and deallocation operators

Please note that all the code examples for this chapter are available as a part of this chapter’s code download on the book’s website at www.wrox.com/go/proc++3e on the Download Code tab.

C++ allows you to redefine the meanings of operators, such as +, -, and =, for your classes. Many object-oriented languages do not provide this capability, so you might be tempted to disregard its usefulness in C++. However, it can be beneficial for making your classes behave similarly to built-in types such as ints and doubles. It is even possible to write classes that look like arrays, functions, or pointers.

Chapters 5 and 6 introduce object-oriented design and operator overloading, respectively. Chapters 7 and 8 present the syntax details for objects and for basic operator overloading. This chapter picks up operator overloading ...

Get Professional C++, 3rd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.