Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One option you could try (I haven't tested this):</p> <pre><code>template&lt;typename T&gt; class MyRef{ private: int index; Polynom&lt;T&gt;*p; public: MyRef(int index, Polynom&lt;T&gt;*p) : index(index), p(p) { } MyRef&lt;T&gt;&amp; operator=(T const&amp;t); //and define these appropriately T operator T() const; }; </code></pre> <p>and define:</p> <pre><code> MyRef&lt;T&gt; operator[](int index){ return MyRef&lt;T&gt;(index, this); } </code></pre> <p>This way when you assign a value to the "reference" it should have access to all the needed data in the polynomial, and take the appropriate actions.</p> <p>I am not familiar enough with your implementation, so I'll instead give an example of a very simple dynamic array that works as follows:</p> <ul> <li>you can read from any <code>int index</code> without concern; elements not previously written to should read off as <code>0</code>;</li> <li>when you write to an element past the end of the currently allocated array, it is reallocated, and the newly allocated elements are initialized to <code>0</code>.</li> </ul> <pre><code>#include &lt;cstdlib&gt; #include &lt;iostream&gt; using namespace std; template&lt;typename T&gt; class my_array{ private: T* _data; int _size; class my_ref{ private: int index; T*&amp; obj; int&amp;size; public: my_ref(T*&amp; obj, int&amp;size, int index) : index(index), obj(obj), size(size){} my_ref&amp; operator=(T const&amp; t){ if (index&gt;=size){ obj = (T*)realloc(obj, sizeof(T)*(index+1) ); while (size&lt;=index) obj[size++]=0; } obj[index] = t; return *this; } //edit:this one should allow writing, say, v[1]=v[2]=v[3]=4; my_ref&amp; operator=(const my_ref&amp;r){ operator=( (T) r); return *this; } operator T() const{ return (index&gt;=size)?0:obj[index]; } }; public: my_array() : _data(NULL), _size(0) {} my_ref operator[](int index){ return my_ref(_data,_size,index); } int size() const{ return _size; } }; int main(){ my_array&lt;int&gt; v; v[0] = 42; v[1] = 51; v[5] = 5; v[5]=6; v[30] = 18; v[2] = v[1]+v[5]; v[4] = v[8]+v[1048576]+v[5]+1000; cout &lt;&lt; "allocated elements: " &lt;&lt; v.size() &lt;&lt; endl; for (int i=0;i&lt;31;i++) cout &lt;&lt; v[i] &lt;&lt; " " &lt;&lt; endl; return 0; } </code></pre> <p>It's a very simple example and not very efficient in its current form but it should prove the point.</p> <p>Eventually you might want to overload <code>operator&amp;</code> to allow things like <code>*(&amp;v[0] + 5) = 42;</code> to work properly. For this example, you could have that <code>operator&amp;</code> gives a <code>my_pointer</code> which defines <code>operator+</code> to do arithmetic on its <code>index</code> field and return a new <code>my_pointer</code>. Finally, you can overload <code>operator*()</code> to go back to a <code>my_ref</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload