mercredi 16 octobre 2019

Constness in brackets operator in custom container

I have a custom class with two overloaded brackets operators -- setter and getter. As you know they look somewhat like this

class IntContainer {
public:
    int const & operator[] (size_t i) const;
    int & operator[] (size_t i);
}

The problem I'm facing now, is that I have to check when the value was set or when it was just accessed, that is I need to track all the changes in my container. It's hard since always only non const operator is called, for example

container[i] = 3;  // Non const operator[] called
x = container[i];  // Again, non const operator[] called

In two cases above I need to differ inner behavior in container. So is there any way to explicitly call different operators in cases like above. I don't want to use const instance of container and to define another functions like set and get, though I'm looking for smoe right design pattern.

Thanks!

Aucun commentaire:

Enregistrer un commentaire