Operator overloading is the process of defining what certain operators mean between user defined types (with certain exceptions). When you first do a "Hello World" in [[C++]], you are using an operator overload defined in the [[Standard Template Library]] for `<<` between the type $\color{lightyellow}\text{std::ostream}$ (Output stream, data type for `std::cout`) and other data types, like $\color{lightyellow}\text{const char}*$, $\color{lightyellow}\text{int}$, $\color{lightyellow}\text{float}$, etc. ```cpp #include <iostream> int main() { std::cout << "Hello World" << std::endl; return 0; } ``` The authors of the standard library overloaded this operator to achieve this syntax to mimic 'piping' data into streams. To define an operator overload between your own [[Class]] and another type, you can create an operator [[Method#Instance Methods|member function]]. An useful example for overloading is when making your own classes based on math, such as a $\color{lightyellow}\text{Vec2}$ class. ```cpp class Vec2 { float x, y; Vec2(float x, float y) : x(x), y(y) {} Vec2 operator+(Vec2 rhs) const { return Vec2(x + rhs.x, y + rhs.y); } } ``` An operator overload method looks identical to a method, with the exception of the naming be `operator` followed by the operator you are overloading. >[!note] Every operator has an allowed number of arguments. For ex. You can't make an operator overload with the signature `Vec2 operator*(Vec2 vec1, Vec2 vec2) because` `*` is a binary operation, so it only acts on 2 values (in the example above it would be [[“This”]] and `Vec2 rhs`). ### List of all possible overloads | Operators that can be overloaded | Examples | Arguments (Including [[“This”]]) | | --------------------------------------------- | ----------------------------- | ------------------------------------------------------------------------------ | | Binary Math | +, -, \*, /, %, &, \|, <<, >> | 2 | | Unary | +, -, ++, —, ~, ^, ! | 1 | | Assignment | =, +=,*=, /=,-=, %= | 2 | | De-referencing | ->, \* | 1 | | Dynamic memory allocation, <br>De-allocation | New, delete | 2, Second argument *must* be `std::size_t` for `new` and `void*` for `delete` | | Subscript | [ ] | 2 | | Function call | () | 1 or More | | Logical | &&,  \|\| | 2 | | Relational | >, < , = =, <=, >= | 2 | >[!warning] Logical Overloads >When overloading `&&` and `||`, they will not short circuit.