In the code above, I know you want to type == not =. But compiler will happily generate code for you, and maybe you have to spend hours to debug. All you need to do is use const to prevent unwanted behavior.

As you can see, the code has two casts, not one. We want the non-const operator[] to call the const one, but if, inside the non-const operator[], we just call operator[], we’ll recursively call ourselves. So we cast *this from TextBlock& to const TextBlock&. The static_cast is to remove the const from the const operator[]‘s return value.