Re: Variables in for loop (style issue)
James Dennett wrote:
> Thorsten Ottosen wrote:
>>struct Foo
>>{
>> const std::vector<int>& bar() const;
>>};
>>
>>In Java, C# and D you can't do it without breaking encapsulation or
>>without copying the entire collection before returning a handle.
>
>
> The idiom in Java is to return a wrapper object, which provides
> an immutable interface and holds a reference to the underlying
> (mutable) collection.
>
> C++'s built-in support for restricting access to only the
> non-mutating part of an interface is a more direct solution,
> but as Java shows, a similar effect can be accomplished in
> a more cumbersome way with more basic features.
>
> (Java's version is also much weaker in that, if memory serves,
> the immutable wrappers actually implement interfaces which
> provide mutator functions, and checking is done at runtime;
> one more way in which type-safety provided at compile-time
> by C++ is deferred to runtime in more dynamic languages.)
I don't know if I will call this an idiom or a hack. I've have not seen
it used very much the places I have worked, because it is just too much
work.
-Thorsten
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]