Re: How to use std::cout to output a char as a number?
On Oct 2, 11:43 pm, Alan Woodland <aj...@aber.ac.uk> wrote:
[snip]
is a technical language newsgroup, and while there are idioms and "best
practices", their merits are not absolute. And not necessarily a priori
known to all.
That said, if I'd suggested a solution to another
(hypothetical) problem in another thread that involved using
printf/malloc or some other older C-style approach without
apparent reason I'm sure it would have attracted criticism.
It would depend on context, I think.
This despite the fact the solution could be an equally valid
answer to the OP. Why would C-style casting syntax in this
case be any different than other C-style functions/syntax?
To phrase my central point as a technical language related question
though: "Why aren't C-style casts marked or viewed as deprecated more? -
Aren't they only in the language now to provide compatibility with C
code, not because they add any useful or tangible benefit to modern C++
developers?"
The whole issue is IMHO somewhat confusing. Personally, I find
it useful to distinguish between "treating an object of type A
as an object of type B", and "creating a new temporary object of
type A, using an expression of type B to initialize it". At
least at a logical level---pointers are also objects, and a
pointer cast does create a new pointer object, but the purpose
of the cast is usually to treat the pointed to object as if it
had a different type. There are several ways to do the first:
you can look at the same bytes (type punning), or you can move
around in an inheritance hierarchy. So we want (and have)
different names for it: reinterpret_cast, dynamic_cast. In the
second case, however, static_cast suggests a "type conversion"
(and that's what the standard calls it), but what I'm
conceptually doing is creating a new object. For a class type,
I'd write A(c), but for the basic types, this doesn't always
work (e.g. unsigned long), so I'll put the typename in
parentheses. So I end up using all three cast notations in the
same program (although const_cast and static_cast are extremely
rare).
It's a question of convention, and I have no trouble adopting
whatever convention the client uses, for "value" conversions
(i.e. conceptually creating new objects). I only insist on the
new style casts for conversions in which the result of the
conversion "refers" to the same object as the orginal---pointer
and reference conversions, in sum.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34