Thread: Old Style C Casts Not Recommended in C++?

  1. #16
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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 or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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, 03:32 AM
  2. Am I using casts properly?
    By Dondrei in forum C++ Programming
    Replies: 7
    Last Post: 03-14-2009, 12: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, 07:06 PM