Re: Cstring, string ,and char[]
On Tue, 19 Jun 2007 10:36:51 -0400, Joseph M. Newcomer
<newcomer@flounder.com> wrote:
It is an unfortunate feature of C++ that most people concentrate on << and >> as if they
have meaning.
For example, "The cool thing about C++ is that you don't need that ugly printf and all
those complicated formatting codes to produce output, you just write cout << and it all
works".
Right.
First, it only works for those objects and classes that have << defined, e.g., cout and
the basic data types. Whoopee. And it only makes sense if a lot of your code deals with
output, which for student projects might be true, but in the real world, output is pretty
irrelevant in terms of code complexity (example: in 130,000 lines of code, I have perhaps
120 uses of CString::Format. That's < 0.1%. And a lot of those are %d, %n.ng, and %08x
for a single value, which I could do without CString::Format but why bother? And have you
REALLY tried to get fancy formatting out of <<? It is MORE complicated than writing %6.2g
by a fair amount!)
<< and >> get their REAL power when you have complicated objects with internal structure.
NOW you can delegate the serialization meaningfully. But to use them for debug print
statements is largely a waste of effort.
The iostream approach is type-safe and extensible. It also can be more
cumbersome than printf. Neither is any good for localization, though Boost
has a library to add format string capabilities to iostreams that includes
localization via positional parameters, similar to CString::FormatMessage,
except I believe it maintains the type-safety of iostreams that is lacking
from CString's ellipsis usage.
I remember one C++ book devoting its first chapter to 'friend' functions, which have
little if anything to do with nearly all of C++; even Stroustrup observed that they were a
hack he added to allow a friend at AT&T to finish his project.
joe
I've never read anything from Stroustrup describing "friend" as a "hack".
ISTR recently posting this:
*****
In "The Design and Evolution of C++", page 53, Stroustrup has this to
say about friendship declarations:
"It is an explicit and specific part of a class declaration.
Consequently, I have never been able to see the recurring assertions
that a friend declaration "violates encapsulation" as anything but a
combination of ignorance and confusion with non-C++ terminology."
http://www.research.att.com/~bs/bs_faq2.html#friend
<q>
Does "friend" violate encapsulation?
No. It does not. "Friend" is an explicit mechanism for granting access,
just like membership. You cannot (in a standard conforming program) grant
yourself access to a class without modifying its source.
</q>
*****
--
Doug Harrison
Visual C++ MVP