Quote:Original post by Sneftel
Also consider this:
*** Source Snippet Removed ***
Which is fine if we just want to inspect that relative. But what if we want to change it? We could make a non-const version of the code (requiring, BTW, the wholesale copy and paste and s/const//g of 18 auxiliary functions, representing 1300 lines of code, YIKES) but that doesn't feel right either, because all this searching in the family tree really SHOULD be guaranteed not to change anything.
What we really want is a way to talk about const-ness context for the family tree as a whole. Either we are a piece of code which is allowed to make babies and give them to people, or we are not. If we are, fine, all the code we need operates on const*s and returns const*s. If we are, however, all the constness in the world should not keep us from controlling Nodes that are under our control.
I've always preferred to think of const as meaning "not only will I not modify anything, I will not even do anything that makes it possible for someone else to modify it".
The problem with the Node example is that the term Node is being overloaded, because a Node really *is* a tree due to the recursive nature of the data structure. So, the second version of the Node data structure seems natural to me, although i admit it's frequently annoying to have to duplicate code in these types of functions.
I actually personally feel like const *should* be taken to mean that you're not allowed to change the state of things it knows about. I guess it depends on what you mean by "know about" though. If you want to make distinctions between statements like:
A : "I know about you, but I don't necessarily care about you as part of my state"
B : "I know about you, and you are an integral part of my behavior. A change to you is a change to me"
Then I think that's actually a legitimate use of the mutable keyword ('mutable' being used for situations of type A).
In your second example, I see again what you're saying but to me it's the same situation. C++'s definition of const means that not only will the function not change anything, it won't *expose* anything either. You shouldn't be able to get access to modify an object's state by calling operations you're allowed to perform if you're only allowed to perform read-only operations.
Take the OP's example (as much as we actually know about the example anyway) as a case in point. He said he's trying to make the same class operate with both Contours and ConstContours. Surely you should not be able to modify a ConstContour right? Otherwise what exactly *is* const about it?
When you really want to make the disctinction between A & B that I described above, I guess there's always the mutable keyword. Although I think I actually find a legitimate use for it less than once a year, I suppose it's possible this is one of those cases. I'd be interested to hear more about this Contour class though, and how he's using the same code to operate on const and non-const contours. I'm positive there's a solution involving some minor meta-programming, or a template specialization, or something that is type-safe as well as const correct, and still allows a list to be used (assuming that's even the best datastructure for the job).