How to overload [] for const object, so that the value assignment would not result in a compilation error : <p>I need to overload [] operator for non-const and const objects. For non-const objects, it should be possible to read and assign values. For const objects, it should be possible to read values, but value assignment should not affect the element. At the same time, the code should be compilable.  </p>

<p>The declarations, that I created:</p>

<pre><code>int &amp;operator[](int element_index);
const int &amp;operator[](int element_index) const;
</code></pre>

<p>The problem with this kind of declarations is, that the following code:</p>

<pre><code>const MyObject some_object(); 
some_object[1] = 100;
</code></pre>

<p>results in a compilation error: "assignment of read-only location" (some_object[1] exists and can be read). The desired behavior is that there would be no compilation error. I tried to find a way to return a copy of an element, but unfortunately, I didn’t find a combination that would work.</p>
