I am writing a template Polynom<T> class where T is the numeric type of its coefficients.
The coefficients of the polynom are stored in an std::vector<T> coefficients, where coefficients[i] corresponds to x^i in a real polynom. (so the powers of x are in increasing order).
It is guaranteed that coefficients vector always contains at least one element. – for a zero polynom it is T().
I want to overload the operator[] to do the following:
- The index passed to the operator[] corresponds to the power of X whose coefficient we want to modify / read.
- If the user wants to just read the coefficient, it should throw for negative indices, return
coefficients.at(i)for indices within the stored range – and reasonably return 0 for all other indices, not throw. - If the user wants to modify 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
coefficients.size(). So we want to somehow resize the vector.
The main problem I have collided with is as follows:
1.
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:
const T& operator[] (int index) const;
T& operator[] (int index);
was insufficient. However, I thought that the compiler would prefer the const version in the read case, won’t it?
2.
I want to make sure that no trailing zeros are ever stored in the coefficients vector. So I somehow have to know in advance, “before” I return a mutable T& of my coefficient, what value user wants to assign. And I know that operator[] doesn’t receive a second argument.
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.
But I cannot do it in advance (before returning a T& from operator[]), 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”.
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.
Could you please advise me as concrete solution as possible to this problem?
I heard something about writing an inner class implicitly convertible to T& with overloaded operator=, but I lack the details.
Thank you very much in advance!
One option you could try (I haven’t tested this):
and define:
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.
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:
int indexwithout concern; elements not previously written to should read off as0;0.It’s a very simple example and not very efficient in its current form but it should prove the point.
Eventually you might want to overload
operator&to allow things like*(&v[0] + 5) = 42;to work properly. For this example, you could have thatoperator&gives amy_pointerwhich definesoperator+to do arithmetic on itsindexfield and return a newmy_pointer. Finally, you can overloadoperator*()to go back to amy_ref.