Bitwise NOT operator

suggest change

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: operator~ returns by value, because it has to return a new value (the modified value), and not a reference to the value (it would be a reference to the temporary object, which would have garbage value in it as soon as the operator is done). Not const either because the calling code should be able to modify it afterwards (i.e. int a = ~a + 1; should be possible).

Inside the class/struct you have to make a temporary object, because you can’t modify this, as it would modify the original object, which shouldn’t be the case.

Feedback about page:

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


Operator overloading:
* Bitwise NOT operator

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