c++ class return reference to itself
<p>Can a c++ object have a method that return a reference to itself?
I want multiple independent instance of the class of the object. </p>

<p>It is for implementing the <code>[]</code> operator of a multi-dimensional array.
I want something like <code>Array[0][1][2]</code> to work.</p>

<p>Thanks.</p>

<p>Update:</p>

<p>Implementation of a multi-dimensional array:</p>

<p>Intended usage:</p>

<pre><code>A[0][1] = 4;    //the element at 0th row and 1st column is now 4.
A[0] = 5;       //all elements of the 0th row are now 5.
A["all"][1] = 10; //all elements of the 1st column are now 10;
</code></pre>

<p>Object A has pointer <code>ptr_buffer</code> to a chunk of memory with correct simd alignment. Constructor of A would allocate the memory. Destructor of A deallocate the memory. A[] returns object B.</p>

<p>Object B has a pointer <code>ptr_A_buffer</code> to a subsection of the memory of A. B[] modifies <code>ptr_A_buffer</code> and also returns reference to itself. I don't want to constantly make a bunch of object B for each [] operation.</p>

<p>Both A and B belong to the same abstract class. Math functions take the abstract class as argument.</p>