What Does It Mean to Overload an Operator?

You have seen a variety of operators in C++, ranging from + and - to +=, <<, and ++. Most of these operators have well-defined meanings and apply to a fairly wide range of types, but none of them applies to user-defined types such as structs and classes. This is not especially surprising. After all, what does it mean to add two instances of aRequest together?

On the other hand, as you saw in the example that used std:string to assemble messages for runtime_error (Lesson 11, “switch Statements, static Variables, and runtime_errors”), it can be very convenient to “add” two objects of a class together. The result in that case was a string object that combined the two string objects from the right and left ...

Get SAMS Teach Yourself C++ in 10 Minutes SECOND EDITION now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.