0

I have a class that accepts STL containers and stores them in a vector. I have a display function that loops through all the elements of all the added containers as if they were appended to each other. This is what I'm trying to emulate with my at() function and the subscript operator overload as well.

template<typename T, typename C = typename T::value_type>
class  MyClass
{
private:
    std::vector<T*> myOwnVectors;

public:
    void add(T* addedContainer)
    {
        myOwnVectors.push_back(addedContainer);
    }

    void displayMyContent() const
    {
        for (auto* j : myOwnVectors)
        {
            for (auto&& i : *j)
            {
                std::cout << i << " ";
            }
            std::cout << std::endl;
        }
    }
    
    int size() const {
        int s = 0;

        for (auto* j : myOwnVectors)
        {
            for (auto&& i : *j)
            {
                s++;
            }
        }

        return s;
    }
    

    C  at(int a) const {

        C result = NULL;
        int numberOfItemsSoFar = 0;
        int numberOfItemsProcessed = 0;

        for (int i = 0; i < myOwnVectors.size(); i++)
        {
            T currentVector = *myOwnVectors.at(i);
            numberOfItemsSoFar += currentVector.size();
            if (a < numberOfItemsSoFar)
            {
                result = currentVector[a-numberOfItemsProcessed];
                break;
            }
            numberOfItemsProcessed += currentVector.size();
        }

        return result;
    }

    C& operator[](int a) {
        C result = NULL;
        int numberOfItemsSoFar = 0;
        int numberOfItemsProcessed = 0;

        for (int i = 0; i < myOwnVectors.size(); i++)
        {
            T currentVector = *myOwnVectors.at(i);
            numberOfItemsSoFar += currentVector.size();
            if (a < numberOfItemsSoFar)
            {
                result = currentVector[a - numberOfItemsProcessed];
                break;
            }
            numberOfItemsProcessed += currentVector.size();
        }
        return result;
    }

    C operator[](int a) const {
        C result = NULL;
        int numberOfItemsSoFar = 0;
        int numberOfItemsProcessed = 0;

        for (int i = 0; i < myOwnVectors.size(); i++)
        {
            T currentVector = *myOwnVectors.at(i);
            numberOfItemsSoFar += currentVector.size();
            if (a < numberOfItemsSoFar)
            {
                result = currentVector[a - numberOfItemsProcessed];
                break;
            }
            numberOfItemsProcessed += currentVector.size();
        }
        return result;
    }
};

I do this in main:

std::vector<double> vek;
vek.push_back(1.2);
vek.push_back(3.2);

MyClass<std::vector<double> > vek2;
vek2.add(&vek);
vek[0] = 4.45;
vek2.displayMyContent();

And this works, the display function displays the correct results with 4.45. However if I keep doing this:

vek2[1] = 6.3;
vek2.displayMyContent();

The change is not visible. Why and how do I change my code so that I can modify vek2 content like this?

  • if you change `vek2[1] = 6.3;` to `vek[1] = 6.3;` it works. but more importantly, assigning `NULL` to a `double` does not do what you think – sp2danny May 29 '22 at 22:44
  • Since your non-`const` version of `operator[]` exhibits undefined behavior (returning a dangling reference -- do you have [compiler warnings enabled](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings)?), there is a chance that your code will start performing exactly as you expect. – JaMiT May 29 '22 at 22:47
  • Does this answer your question? [Overloading operator \[\] for both read and write](https://stackoverflow.com/questions/8393059/overloading-operator-for-both-read-and-write) – JaMiT May 29 '22 at 22:54
  • http://coliru.stacked-crooked.com/a/96879f479883c5ca – sp2danny May 29 '22 at 22:58

0 Answers0