I have a template class, called OneCell, here the definition:
template <class T>
class OneCell
{
.....
}
I have a cast operator from OneCell to T, here
operator T() const
{
getterCount++;
return value;
}
As you see, I want to increment variable in this method, but i get an error because the keyword const.
On the other hand, if I remove this keyword, the casting doesn’t work at all.
What can I do?
Thank you, and sorry about my poor English.
Actually
operator T() constis a const-member function, inside whichthispointer refers to aconstobject, which in turns makes the getterCountconstas well.The solution is to declare
getterCountas mutable, as shown below:Now
getterCountcan be incremented in const member function, which also means, it can be incremented even if the object isconst: