Operator Overloading

suggest change

Introduction

In C++, it is possible to define operators such as \+ and -> for user-defined types. For example, the <string> header defines a \+ operator to concatenate strings. This is done by defining an operator function using the operator keyword.

Remarks

The operators for built-in types cannot be changed, operators can only be overloaded for user-defined types. That is, at least one of the operands has to be of a user-defined type.

The following operators cannot be overloaded:

There are some operators that you should not (99.98% of the time) overload:

Why? Because they overload operators that another programmer might never expect, resulting in different behavior than anticipated.

For example, the user defined && and || overloads of these operators lose their short-circuit evaluation and lose their special sequencing properties (C++17), the sequencing issue also applies to , operator overloads.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Operator overloading:
* Operator Overloading

Table Of Contents
8 Arrays
11 Loops
39 Streams
51 Unions
55 Operator overloading
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype