void destroy()
{
AList::const_iterator a;
for(a = AList.begin(); a != AList.end();)
{
if(!a->second.BList.empty())
a->second.BList.clear();//will give error if not mutable
}
}
typedef std::map<unsigned int,int> bmap;
typedef std::map<unsigned int,someStruct> Alist;
typedef struct someStruct
{
float x,y,z;
bmap BList; //needs to be mutable for Blist.clear() above.
//mutable bmap BList; //<---like this
} someStruct;
I only chanced across mutable as an option in a similar but not the same question. My question is am I doing the right thing, or if there are any pitfalls in doing so? Thank you for your help in advance.
//error given: (if otherwise not mutable)
// error: passing 'const AList' as 'this' argument of 'void std::map<_Key, _Tp, _Compare, _Alloc>::clear() [with _Key = unsigned int, _Tp = int, _Compare = std::less<unsigned int>, _Alloc = std::allocator<std::pair<const unsigned int, int> >]' discards qualifiers
You should use
iteratorinstead ofconst_iterator, if your intent is to callclear.const_iteratoris for cases where you are calling onlyconstmember functions.Using
mutableis not appropriate for this situation. Only mark member variablesmutableif they are not part of the object’s visible state, e.g., cached data.