Tutorial by Examples

You can overload all basic arithmetic operators: + and += - and -= * and *= / and /= & and &= | and |= ^ and ^= >> and >>= << and <<= Overloading for all operators is the same. Scroll down for explanation Overloading outside of class/struct: //operator...
You can overload the 2 unary operators: ++foo and foo++ --foo and foo-- Overloading is the same for both types (++ and --). Scroll down for explanation Overloading outside of class/struct: //Prefix operator ++foo T& operator++(T& lhs) { //Perform addition return lhs; } ...
You can overload all comparison operators: == and != > and < >= and <= The recommended way to overload all those operators is by implementing only 2 operators (== and <) and then using those to define the rest. Scroll down for explanation Overloading outside of class/struct: ...
You can overload type operators, so that your type can be implicitly converted into the specified type. The conversion operator must be defined in a class/struct: operator T() const { /* return something */ } Note: the operator is const to allow const objects to be converted. Example: struct ...
You can even overload the array subscript operator []. You should always (99.98% of the time) implement 2 versions, a const and a not-const version, because if the object is const, it should not be able to modify the object returned by []. The arguments are passed by const& instead of by value...
You can overload the function call operator (): Overloading must be done inside of a class/struct: //R -> Return type //Types -> any different type R operator()(Type name, Type2 name2, ...) { //Do something //return something } //Use it like this (R is return type, a and b a...
The assignment operator is one of the most important operators because it allows you to change the status of a variable. If you do not overload the assigment operator for your class/struct, it is automatically generated by the compiler: the automatically-generated assignment operator performs a &qu...
Overloading the bitwise NOT (~) is fairly simple. Scroll down for explanation Overloading outside of class/struct: T operator~(T lhs) { //Do operation return lhs; } Overloading inside of class/struct: T operator~() { T t(*this); //Do operation return t; } Note...
The operators << and >> are commonly used as "write" and "read" operators: std::ostream overloads << to write variables to the underlying stream (example: std::cout) std::istream overloads >> to read from the underlying stream to a variable (example: s...
The code below implements a very simple complex number type for which the underlying field is automatically promoted, following the language's type promotion rules, under application of the four basic operators (+, -, *, and /) with a member of a different field (be it another complex<T> or so...
You can extend C++ with named operators that are "quoted" by standard C++ operators. First we start with a dozen-line library: namespace named_operator { template<class D>struct make_operator{constexpr make_operator(){}}; template<class T, char, class O> struct half_a...

Page 1 of 1