Let's say I have a class:
class NumberCollection
{
public:
typedef std::set<int> SetType;
typedef SetType::iterator iterator;
void insert(int n);
iterator begin();
iterator end();
size_t size() const;
iterator difficultBegin();
iterator difficultEnd();
size_t difficultSize() const;
private:
SetType easySet_, difficultSet_;
}
Where insert()
adds an element to easySet_
. difficultSet_
's members change depending on the members of easySet_
.
The problem I am having is that, multiple insertions means that difficultSet_
is constantly recalculated. So I want difficultSet_
to be calculated lazily (i.e., only when difficultBegin()
, difficultEnd()
, or difficultSize()
are called). The problem is, then I actually have to make difficultSet_
into a mutable
because otherwise difficultSize()
cannot operate on it.
So now my class declaration looks like
class NumberCollection
{
public:
typedef std::set<int> SetType;
typedef SetType::iterator iterator;
void insert(int n);
iterator begin();
iterator end();
size_t size() const;
iterator difficultBegin();
iterator difficultEnd();
size_t difficultSize() const;
private:
SetType easySet_;
mutable SetType difficultSet_;
mutable bool upToDate_;
}
I feel like this is bad design though. Is there a better way?