Thread: Advantages of c++ type casting over c type casting

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    3

    Question Advantages of c++ type casting over c type casting

    Hi friends,

    This is the my first post to the forum. I want to know what is the need of (advantage of)

    static_cast and reinterpret_cast in c++ . we can do all these work through simple c type casting.

    I mean if i am writing
    Code:
    class a ;
    class b;
    
    a *aptr = new a;
    
     b = reinterpret_cast <b*> (&a);
    
    ALso same thing i can do like 
    
    b = (b*) (&a);

    Please let me know if my query is not clear....

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Read Stroustrup's answer to the FAQ: What good is static_cast?
    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

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are several differences (although I couldn't enumerate all of them)...
    The most obvious ones are:
    1. You tell the user what you actually wanted to to.
    2. The compiler is at least SOMETIMES capable of telling you when you are doing daft things that you shouldn't be doing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Because b is an invalid unusable pointer, and if you used static_cast (reinterpret_cast is only for cases where you are willing to take full responsibility and know exactly what you are doing), the compiler would refuse.

    There is also a FAQ board where this topic is currently the latest.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The intention of the code is more explicit with C++ casts (as each cast has a different intended usage) and it's easier to search for casts that have a unique syntax. I can't say I'm a fan of C++ casts because they were intentionally designed to discourage casting. I have no problem with discouraging casting, but it irks me that they would use a deceptive trick and try to palm it off as a good thing.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    3
    Thanks all guys for ur sugestions.

    1- Hi laserlight ur link is quite promising. After going through the material I don't find much difference except that c++ type casting let know the programmer bit boldly that u may going to fall in a danger area. and it may help o trace such things in a big programme But at the same time u can see the complexity of the code increase. R u agree with me.


    2- hi anon u r right but here my intesion is to go into danger therefore i am writing the cast code i don't want to get a error using static_cast rather i will always go for reinterpret cast or c style cast (the simple one).

    3- hi matsp i thnk u r wrong at this point
    Code:
    You tell the user what you actually wanted to to.
    Ithink user of a aplication is ignore abt the code so here the developer is all in all and he knew what he is going to do.

    Correct me if i am wrong and feel free to put ur views.

    Thanks

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kaibalya2008
    But at the same time u can see the complexity of the code increase.
    The code would be more verbose, yes, though not more complex. It may even be less "complex" since there is less to guess about the author's intentions.

    Quote Originally Posted by kaibalya2008
    but here my intesion is to go into danger therefore i am writing the cast code i don't want to get a error using static_cast rather i will always go for reinterpret cast or c style cast (the simple one).
    But that does not make sense: why would your intention be to court danger? You are programming, not bungee jumping.

    Quote Originally Posted by kaibalya2008
    Ithink user of a aplication is ignore abt the code so here the developer is all in all and he knew what he is going to do.
    I think matsp meant reader, or maybe a user of your class or function who is investigating how it works.
    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

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    3
    Quote Originally Posted by laserlight View Post
    The code would be more verbose, yes, though not more complex. It may even be less "complex" since there is less to guess about the author's intentions.
    Yes u r right , In my mind that was verbose not complex .

    But that does not make sense: why would your intention be to court danger? You are programming, not bungee jumping.
    But don't know why there is an option for bungee jumping in c++



    I think matsp meant reader, or maybe a user of your class or function who is investigating how it works.
    yes u may be right .

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    but here my intesion is to go into danger therefore i am writing the cast code i don't want to get a error using static_cast rather i will always go for reinterpret cast or c style cast (the simple one).
    Judging from your writing style, you won't probably come out alive.

    I would guess around 99% of the time you don't want a reinterpret_cast.

    But don't know why there is an option for bungee jumping in c++
    Because there may be 1% of cases where it is useful. The aim of C++ is not to insulate the programmer from low-level details, but it does give you the means to avoid them most of the time.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The FAQ board topic anon referred to.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Use static_cast first. If the compiler yells at you, double check to make sure you're not doing something crazy; then if you're sure, change it to reinterpret_cast.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. help with simple type casting problem
    By Jeremy_S in forum C Programming
    Replies: 2
    Last Post: 02-27-2002, 12:38 PM