a simple piece of code:
class HasPtr
{
public:
HasPtr(const string& s = string()) :ps(new string(s)), i(0), use(new size_t(1)) {}
HasPtr(const HasPtr& p) :ps(p.ps), i(p.i), use(p.use) { ++* use; }
HasPtr& operator=(const HasPtr&);
~HasPtr();
private:
string* ps;
int i;
size_t* use;
};
HasPtr& HasPtr::operator=(const HasPtr& rhs)
{
++* rhs.use;
if (-- * use == 0) {
delete ps;
delete use;
}
ps = rhs.ps;
i = rhs.i;
use = rhs.use;
return *this;
}
The parameter of the copy assignment operator is const.c++ primer says A const type can use most but not all of the same operations as its nonconst version.The one restriction is that we may use only those operations that cannot an object. Why can the value pointed to by use(++* rhs.use) be modified here? Does this violate the rules for const properties?