Re: Variables in for loop (style issue)
Anders J. Munch wrote:
Thorsten Ottosen wrote:
Anders J. Munch wrote:
D has less need for the const/non-const distinction than C++
because fully-immutable objects are more efficient.
That certainly depend on the object. It's not feasible, for example,
to have an immutable container, say, like std::vector<T>.
It is quite feasible to work with immutable containers. Haven't you
ever tried functional programming?
I haven't used much time in pure functional languages.
Anyway, none of these languages seems to be performance
oriented.
In Java, C# and D you can't do it without breaking encapsulation or
without copying the entire collection before returning a handle.
Pick your poison.
OK, here's my poison:
class Bar
{
// .. implementation left as an exercise to the reader
public:
Bar(Bar const&);
template<typename Iterator>
Bar(Iterator begin, Iterator end);
/* Using index-based access instead of iterators in the
interest of keeping the example simple. */
int operator[](size_t index) const;
size_t size() const;
}:
struct Foo
{
Bar bar() const;
};
Since this is C++, const annotations are included for good measure, but
in Bar they do not serve their intended purpose of blocking access to
mutation, for Bar has no mutating operations to block.
So?
The copy constructor is a potential inefficiency,
but we haven't seem the state of Bar, so we don't know that?
Also, if bar is expensive to copy, then return a cnost reference.
And take into account NRVO and RVO.
so I might need to use
shared_ptr at some level. In a language with garbage-collected
object-references, the copy constructor would disappear.
Copying data is good for excapsulation.
However, you still haven't decided what you want to give up:
1. encapsulation, or
2. performance
showing a class that has no mutating operations don't really
by your arguments much because many classes don't fall into that
category.
-Thorsten
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]