Old Style C Casts Not Recommended in C++?

This is a discussion on Old Style C Casts Not Recommended in C++? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by VirtualAce To be fair I do not think auto_ptr was intended to be as robust as boost::shared_ptr. ...

  1. #16
    Registered User
    Join Date
    Jun 2005
    Posts
    6,529
    Quote Originally Posted by VirtualAce View Post
    To be fair I do not think auto_ptr was intended to be as robust as boost::shared_ptr.
    std::auto_ptr is fine as long as it is used for what it was designed for: providing lightweight management of the life of a single object. A boost::shared_pointer is effectively a reference counted pointer, which is heavy weight compared to a std::auto_ptr. That difference of robustness you suggest is caused by programmers who keep using auto_ptr as if it was a reference countered pointer, which it never was. From a design perspective, auto_ptr should never have supported any copy semantics, rather than supporting weird copy semantics, but that's another story.

    As to original question, the use of any cast is generally best avoided in C++, whether the old-style C casts or the C++ "new style" casts. In fact their long names (dynamic_cast, reinterpret_cast, static_cast, const_cast) were deliberately chosen to make them a little inconvenient to use, and to act as an alert to revisit code design to avoid them if at all possible.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,157
    Quote Originally Posted by grumpy
    From a design perspective, auto_ptr should never have supported any copy semantics, rather than supporting weird copy semantics, but that's another story.
    Yeah, one of my main uses for std::auto_ptr was in the role of boost::scoped_ptr, though now std::unique_ptr takes over that role.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C-style casts removed
    By KIBO in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2009, 04:32 AM
  2. Am I using casts properly?
    By Dondrei in forum C++ Programming
    Replies: 7
    Last Post: 03-14-2009, 01:51 AM
  3. casts
    By BlaX in forum C Programming
    Replies: 2
    Last Post: 03-08-2008, 02:11 PM
  4. Overloading casts
    By CodeMonkey in forum C++ Programming
    Replies: 2
    Last Post: 12-24-2006, 11:11 PM
  5. Templates and type casts
    By ventolin in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2004, 08:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21