J
Jeff Schwab
How can you tell with a toy example?
I can't. That's what I meant by "you are correct."
I can envision examples where
iterators would be appropriate (like for a container),
Right, agreed.
where raw
arrays would be appropriate (like for a 3D vector returning
coordinates to be rendered)
The reason to use raw arrays in such a case is to enable rapid I/O by
allowing very low-level data access. In such a case, the client code
probably should (and generally does) own the array, and pass down a
pointer-to-buffer; as an example, see the fread function. The OP
included a "getArr" function with similar semantics.
The common use case for returning a pointer to a private array is to
provide the caller with occasional access to a buffer that may be (but
is not necessarily) populated already with whatever data the client
wants. Examples include std::string::c_str and strerror. I'm not
personally a big fan of this technique, because there is the danger that
the client will hang onto the pointer after the array's contents have
been invalidated, but continue to use it as though it still pointed to
the original data. There is no way for us to warn the client when they
make such a mistake; all we can do is shrug, and point to the
documentation. The raw pointer is just too low-level a data type to
serve as a reliable handle.
and for complete encapsulation. The
example provided didn't give enough detail to determine which course
to take, so I defaulted to protecting encapsulation.
What do you mean by "protecting encapsulation?" I'm not arguing with
you, I just don't understand what you mean by that phrase.