const and immutable member variables in classes
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Feb 2 19:32:39 PST 2016
On 02/02/2016 07:05 PM, Jonathan M Davis via Digitalmars-d-learn wrote:
> Well, in principle, if you have a member that's going to be
initialized on
> construction and then never mutated, having it be const or immutable
would
> be desirable,
That's how I've seen it used in a friend's code. Makes sense to me as
long as it's not a struct.
> but for all of the reasons that you're listing, to do so with
> structs is ultimately a bad idea.
Agreed.
> But under normal
> circumstances, member variables of classes don't have that problem,
because
> you don't normally ever try to overwrite the state of the whole class
> object.
Exactly my point.
> I usually tell folks not to do it because of
> all of the problems it causes.
That's been my guideline: "const and immutable members should be
avoided." Now I'm tempted to add this: "only in structs."
Ali
More information about the Digitalmars-d-learn
mailing list