old style casts vs new style casts 
Author Message
 old style casts vs new style casts


Quote:
> First, what is exactly "old style cast"? Why are they worse than new style
> casts like static_cast? Because they don't throw exceptions? Why
> reinterpret_cast still used in ATL7.0 source code even though MSDN
> recommends to use it as a last resort?

(int)1.0;    // old-style cast

They're "worse" than the new casts because they're too sweeping - the new
casts let you state what you're trying to do, and the compiler will oblige
only if you're trying to do something sensible.

Whow knows why ATL does anything it does?  ;-)  Seriously though,
reinterpret_cast is still useful - it just needs to be used with caution.

-cd



Sun, 27 Feb 2005 02:42:20 GMT  
 old style casts vs new style casts
First, what is exactly "old style cast"? Why are they worse than new style
casts like static_cast? Because they don't throw exceptions? Why
reinterpret_cast still used in ATL7.0 source code even though MSDN
recommends to use it as a last resort?


Sun, 27 Feb 2005 02:33:03 GMT  
 old style casts vs new style casts

Quote:
> First, what is exactly "old style cast"?

Old-style cast, aka C-style cast, is a construct of the form
(Type)expression, as in

int i;
char *p = (char*)i;

Quote:
> Why are they worse than new style casts like static_cast?

Because new-style casts show your intention more clearly, and you are
less likely to do more than you have intended. You can cast away
constness with const_cast or with old-style cast, but const_cast is
clearer, essentially self-documenting, and won't allow you to
accidentally cast to a different type.

Quote:
> Because they don't throw exceptions?

Only dynamic_cast can throw exceptions, and it is in a different league
anyway, because there is no old-style equivalent.

Quote:
> Why reinterpret_cast still used in ATL7.0 source code even though MSDN
> recommends to use it as a last resort?

reinterpret_cast is often unavoidable in Windows programming. E.g. many
Windows messages expect pointers passed as LPARAM. COM functions often
have void** parameter where you need to pass SomeInterface**.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Sun, 27 Feb 2005 02:43:36 GMT  
 old style casts vs new style casts
There are two articles in Bobby Schmidt's Deep C++ column.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnde...
l/deep08272002.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnde...
l/deep08272002.asp

When he compares static_cast and reinterpret_cast he writes:

Overlap
There is some overlap in the behavior of reinterpret_cast and static_cast.
Both forms
Convert between void * and other pointer types
Convert between base and derived pointers
Bind a derived reference to a base object
Where either form will work, I recommend you stick with reinterpet_cast:
The name reinterpret_cast more precisely describes the transformation.
static_cast is one of the most dangerous tools in the C++ shed. You should
reach for it only when nothing else will work.

Especially the last sentence makes me wonder. I always thought that
reinterpret_cast should be used as a last resort. Would anyone care to
comment.


Quote:
> First, what is exactly "old style cast"? Why are they worse than new style
> casts like static_cast? Because they don't throw exceptions? Why
> reinterpret_cast still used in ATL7.0 source code even though MSDN
> recommends to use it as a last resort?



Sun, 27 Feb 2005 03:20:40 GMT  
 old style casts vs new style casts

Quote:

> There are two articles in Bobby Schmidt's Deep C++ column.

> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnde...
> l/deep08272002.asp

> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnde...
> l/deep08272002.asp

[...]

Quote:
> Where either form will work, I recommend you stick with reinterpet_cast:
> The name reinterpret_cast more precisely describes the transformation.
> static_cast is one of the most dangerous tools in the C++ shed. You should
> reach for it only when nothing else will work.

This is backwards.  static_cast is much more limited, and as far as I
know, it's possible to get an unintended value with it but not really
possible to break a program outright.  reinterpret_cast is hazardous,
and may break things badly if it's not used properly.


Sun, 27 Feb 2005 03:48:04 GMT  
 old style casts vs new style casts

Quote:

> First, what is exactly "old style cast"? Why are they worse than new style
> casts like static_cast? Because they don't throw exceptions?

No, because they do a number of different things, and if you're not
careful, when you intend to do one thing (that's safe), you'll
accidentally do something else (that's unsafe).  The new-style casts
are much more specific about what it is that you're doing and won't
automagically shift from something safe (static_cast) to something
less safe (const_cast) or unsafe (reinterpret_cast) if the safe
option isn't allowed.

Quote:
> Why
> reinterpret_cast still used in ATL7.0 source code even though MSDN
> recommends to use it as a last resort?

"last resort" isn't, IMO, strictly accurate.  And, I'm sure it's used
because that's what's appropriate in that context.  Whereas ATL has to
interact with a C-based API that implements OO stuff, it's
understandable that reinterpret_casts would be necessary to work with
the void*'s.


Sun, 27 Feb 2005 03:51:55 GMT  
 old style casts vs new style casts

Quote:
> This is backwards.  static_cast is much more limited, and as far as I
> know, it's possible to get an unintended value with it but not really
> possible to break a program outright.

Sure is possible, by downcasting when the pointer is not actually to a
derived class:

struct Base {};
struct Derived : public Base
{
    int n;
    void f() {n = 1;}

Quote:
};

Base* pB = new Base;
Derived* pD = static_cast<Derived*>(pB);
pD->f(); // buffer overrun

Another unsafe conversion is from void* to any pointer, which allows one
to cast between pointers to unrelated types:

Class1* p1;
void* pv = p1;
Class2* p2 = static_cast<Class2>(pv);

But of course I agree that reinterpret_cast is much more handy to shoot
oneself in the foot with.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Sun, 27 Feb 2005 03:57:57 GMT  
 old style casts vs new style casts
For a comparison of various casts and when/where to use them, see :-

http://www.codeproject.com/managedcpp/castingbasics.asp

Nish


Quote:
> First, what is exactly "old style cast"? Why are they worse than new style
> casts like static_cast? Because they don't throw exceptions? Why
> reinterpret_cast still used in ATL7.0 source code even though MSDN
> recommends to use it as a last resort?



Sun, 27 Feb 2005 09:15:31 GMT  
 old style casts vs new style casts
Aand have a look at this one from R.C. Morin (who appears to have vanished).

This one is provides a rather introductory look at casting, the web site is
disgustingly commercialized, but the technical matter is still quite good.

http://www.kbcafe.com/articles/casting.html

regards
roy fine


Quote:
> For a comparison of various casts and when/where to use them, see :-

> http://www.codeproject.com/managedcpp/castingbasics.asp

> Nish



> > First, what is exactly "old style cast"? Why are they worse than new
style
> > casts like static_cast? Because they don't throw exceptions? Why
> > reinterpret_cast still used in ATL7.0 source code even though MSDN
> > recommends to use it as a last resort?



Sun, 27 Feb 2005 09:58:04 GMT  
 old style casts vs new style casts
http://www.cs.rpi.edu/~wiseb/xrds/ovp3-1.html

i hope it helps


Quote:
> First, what is exactly "old style cast"? Why are they worse than new style
> casts like static_cast? Because they don't throw exceptions? Why
> reinterpret_cast still used in ATL7.0 source code even though MSDN
> recommends to use it as a last resort?



Sun, 27 Feb 2005 10:22:35 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. old style casts vs new style casts

2. Need help with reinterpret_cast, a C-style cast or function-style cast

3. Need help with reinterpret_cast, a C-style cast or function-style cast

4. Need help with reinterpret_cast, a C-style cast or function-style cast

5. Need help with reinterpret_cast, a C-style cast or function-style cast

6. new style declarations OK for old style definitions?

7. old C style to new C style conversion

8. Convert old-style func decl to ANSI-style?

9. C++ style cast

10. static_cast or C-styled cast?

11. Converting old style functions to new

12. Conversion between old and new-style source code

 

 
Powered by phpBB® Forum Software