On Nov 21, 9:58 pm, Jonathan Jones <clcppm-pos...@this.is.invalid>
wrote:
I guess the problem that lead to my original question (wanting
assignment to work with const members), is that C++ doesn't fully
support it. What I _really_ want is this:
struct Object
{
Object(int data, ...) : data_(data), ... {}
Object(const Object& r) : data_(r.data_), ... {}
foo() { ... } // can modify everything except data_
bar() { ... } // can modify everything except data_
baz() { ... } // can modify everything except data_
Object& operator=(const Object& r) { data_=r.data; ... }
private:
const int data_;
// lots of non-const data
};
I know you could const-qualify all the member functions, while
declaring all the other data as mutable, but that seems ugly at
best.
Interesting idea. Perhaps utilizing keyword mutable to specify that
member function can change const data members:
struct Object
{
foo() { ... } // can modify everything except data
bar() const { ... } // can't modify anything
baz() mutable { ... } // can modify everything including data
Object& operator=(const Object& r) mutable { data=r.data; ... }
const int data;
};
This also provides information compiler needs to keep optimizing
const data access when possible, but also know when it needs to
reload const values (after call to any mutable member function).
access rules.
[ comp.lang.c++.moderated. First time posters: Do this! ]