mutable reference to const object
FreeSlave
freeslave93 at gmail.com
Thu Mar 6 11:15:28 PST 2014
Yes, in some cases C++ const does not provide const-guarantees,
it's more like convention.
For example, you can write this:
class Class
{
public:
Class() : ptr(new int()) {}
~Class() { delete ptr; }
void setData(int data) const {
*ptr = data;
}
private:
int *ptr;
};
There is interesting feature here. You can create instances that
are const by its structure, but not by data. So const-method can
not change size of array (reallocate), but may change data. It
may be useful, for example, if you want to allow to change data
in graph or another data structure, but want to disallow to
add/remove vertices. Though it surely needs other definition
instead of const.
More information about the Digitalmars-d-learn
mailing list