Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6784969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:03:58+00:00 2026-05-26T17:03:58+00:00

I am writing a template Polynom<T> class where T is the numeric type of

  • 0

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:

  1. The index passed to the operator[] corresponds to the power of X whose coefficient we want to modify / read.
  2. 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.
  3. 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:03:59+00:00Added an answer on May 26, 2026 at 5:03 pm

    One option you could try (I haven’t tested this):

    template<typename T>
    class MyRef{
    private:
       int index;
       Polynom<T>*p;
    public:
        MyRef(int index, Polynom<T>*p) : index(index), p(p) { }
    
        MyRef<T>& operator=(T const&t); //and define these appropriately
        T operator T() const;         
    };
    

    and define:

        MyRef<T> operator[](int index){
            return MyRef<T>(index, this);
        }
    

    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:

    • you can read from any int index without concern; elements not previously written to should read off as 0;
    • 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 0.
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    
    template<typename T>
    class my_array{
    private:
        T* _data;
        int _size;
    
        class my_ref{
    
            private:
                int index;
                T*& obj;
                int&size;
            public:
                my_ref(T*& obj, int&size, int index)
                    : index(index), obj(obj), size(size){}
    
                my_ref& operator=(T const& t){
    
                    if (index>=size){    
                        obj = (T*)realloc(obj, sizeof(T)*(index+1) );
                        while (size<=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& operator=(const my_ref&r){              
                    operator=( (T) r);
                    return *this;
                }
    
                operator T() const{
                    return (index>=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<int> 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 << "allocated elements: " <<  v.size() << endl;
        for (int i=0;i<31;i++)
            cout << v[i] << " " << endl;
    
        return 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 that operator& gives a my_pointer which defines operator+ to do arithmetic on its index field and return a new my_pointer. Finally, you can overload operator*() to go back to a my_ref.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a template class which should take an array as its type. public
I am writing a template class which takes a floating-point-like type (float, double, decimal,
Hey there, I'm writing a template container class and for the past few hours
I'm writing a template class and at one point in my code would like
I am busy writing a CodeSmith template that has one of its properties as
I'm writing a simple hash map class: template <class K, class V> class HashMap;
I am writing a template class for an array of objects, call it arrayobjclass,
So in writing a C++ template class, I have defined a method that returns
I am writing a template class that takes as an input a pointer and
I'm writing a template function which should swap two elements of a boost::mpl::vector (similarly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.