Re: Variables in for loop (style issue)
thorsten.ottosen@dezide.com (Thorsten Ottosen) wrote (abridged):
It's not feasible, for example, to have an immutable container,
say, like std::vector<T>.
Actually it is. For example, Java's string class is immutable. Some
of us
wish C++ had an immutable string class too.
[return by const reference example snipped]
In Java, C# and D you can't do it without breaking encapsulation or
without copying the entire collection before returning a handle.
To do the equivalent in Java you would need to write the const
version of
the interface by hand. For example:
struct ConstPoint {
int x();
int y();
// ...
};
struct Point: ConstPoint {
int set_x();
int set_y();
void operator=( ConstPoint &rhs );
// ...
};
struct Rect {
ConstPoint &top_left() { return top_left_; }
void set_top_left( ConstPoint &tl ) { top_left = tl; }
//...
private:
Point top_left_;
};
From a clients point of view, there's not much difference between
"const
Point" and "ConstPoint".
"Const" is a type operator. It creates a new type from an old one by
stripping out the non-const member functions. You can write the same
interface by hand.
-- Dave Harris, Nottingham, UK.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]