Re: Variables in for loop (style issue)
Thorsten Ottosen wrote:
Without const we can choose between
1. bad encapsulation (we return a mutable reference)
2. bad performance (we return a copy or the value type is immutable)
Pick your poison
Sadly, I consider encapulation broken anyway. With that const on the
end of the function, you have exposed implementation detail to the user
and for no better reason than to allow the compiler to know that the
object doesn't happen to be modified and can be called on a const
object. I am not sure of the correct solution to this, but it somewhat
irritates me to have to expose implementation details and sometimes
implement the same routine twice just to satisfy const correctness.
Well, there seems to be several views on encapsulation:
1. you don't know/care what data members the class has
2. you can't modify the class' invariant without using a member function
The first is of minimal importance, the second is of *immense* importance.
There is quite some misunderstanding in academia
and in general anout the virtues of encapsulation.
(Partly because people don't understand what a class invariant is).
Often we see either over-encapsulation or broken encapsulation. And
the net result is that in languages without const, encapsulation is
often broken because references to internal data is given up. The
trouble of either
1. desiging Copy-on-write, or
2. immutable/mutable companions (Java's strimg/string buffer)
is so great that it remains a library solution. Very few users will
employ it in their own production code.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]