Note that there are some explanatory texts on larger screens.

plurals
  1. POOverloading operator[] for a template Polynom class
    primarykey
    data
    text
    <p>I am writing a template <code>Polynom&lt;T&gt;</code> class where <code>T</code> is the numeric type of its coefficients.</p> <p>The coefficients of the polynom are stored in an <code>std::vector&lt;T&gt; coefficients</code>, where <code>coefficients[i]</code> corresponds to <code>x^i</code> in a real polynom. (so the powers of x are in increasing order). </p> <p>It is guaranteed that <code>coefficients</code> vector always contains at least one element. - for a zero polynom it is <code>T()</code>.</p> <p>I want to overload the <code>operator[]</code> to do the following:</p> <ol> <li>The index passed to the operator[] corresponds to the power of X whose coefficient we want to modify / read.</li> <li>If the user wants to just <strong>read</strong> the coefficient, it should throw for negative indices, return <code>coefficients.at(i)</code> for indices within the stored range - and <strong>reasonably</strong> return 0 for all other indices, not throw.</li> <li>If the user wants to <strong>modify</strong> the coefficient, it should throw for negative indices, but let user modify all other indices freely, even if the index specified is bigger than or equal to <code>coefficients.size()</code>. So we want to somehow resize the vector.</li> </ol> <p>The main problem I have collided with is as follows:</p> <p>1.</p> <p>How do I distinguish between the read case and the write case? One person left me without an explanation but said that writing two versions:</p> <pre><code>const T&amp; operator[] (int index) const; T&amp; operator[] (int index); </code></pre> <p>was insufficient. However, I thought that the compiler would prefer the const version in the read case, won't it? </p> <p>2.</p> <p>I want to make sure that no trailing zeros are ever stored in the <code>coefficients</code> vector. So I somehow have to know in advance, "before" I return a mutable <code>T&amp;</code> of my coefficient, what value user wants to assign. And I know that <code>operator[]</code> doesn't receive a second argument.</p> <p>Obviously, if this value is not zero (not T()), then I have to resize my vector and set the appropriate coefficient to the value passed.</p> <p>But I cannot do it in advance (before returning a <code>T&amp;</code> from <code>operator[]</code>), because if the value to be assigned is T(), then, provided I resize my coefficients vector in advance, it will eventually have lots of trailing "zeroes".</p> <p><em>Of course I can check for trailing zeroes in every other function of the class and remove them in that case. Seems a very weird decision to me, and I want every function to start working in assumption that there are no zeroes at the end of the vector if its size > 1.</em></p> <p>Could you please advise me as concrete solution as possible to this problem? I heard something about writing an inner class implicitly convertible to <code>T&amp;</code> with overloaded <code>operator=</code>, but I lack the details. </p> <p>Thank you very much in advance!</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. 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