Thread: reinterpret vs c-style cast

  1. #46
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Honestly, I have yet to see well-designed code that used casts at all. Or is that just me? Name any design pattern or function where you'd need to cast, which couldn't designed better without casts...

  2. #47
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, what you say is true. But I don't know from the beginning which function pointer does what thing. More specific I don't know the group of the function pointers from the beginning. So If I start writing a function and I am like "hey, I want also this as a parameter", it is more easy casting than changing the struct. Anyway, it is just an example. The real objection is that for me it is easier. Personally, I would create more bugs when writing something not that readable rather than miss casting.
    Polymorphism can be achieve with casting and virtual functions. Polymorphism is really important so making one of the two ugly doesn't seem as a good idea.

    Pointers are not safe either, but C++ chose to have them. The point is that I would like for a language to provide a nice way to do things the way you want to. It is ok to provide safety for something that somebody can use without knowing what he is doing. But for something that you are aware of, like the effects of casting, it should be easy to use.

  3. #48
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Quote Originally Posted by C_ntua View Post
    And you say in Java you cast all the time. What is the difference in C++?
    Too much to bring up. You can read a lot of the Java vs C++ replies, if you want. There are a few.

    Quote Originally Posted by C_ntua View Post
    Here is a personal example. I want to have a function pointer. One function pointer that generally points to functions that can do a lot of things. So the function pointer is a type of returning void and accepting a list of void* pointers. Depending on the function the pointer points I cast the elements of the list in appropriate type. There are some reasons of course I chose to do this. And yes I know it would be easy to miss-cast or if I changed something I would have to change a lot of things. But still I chose to do so. So why wouldn't a language, a tool, provide me a default easy and readable way to do so? I used C-style casts, because they were more readable. It would have been better to use the other casts, because of the fact that they are more safe. That is the thing. I would expect something unsafe to be less readable than something safe. And the readability of the code makes you spot more easily other bugs, more common that miss-casts
    There are a lot of solutions to that one. The one that comes to mind is templates. Make the functions accept a pointer of type T. Then it can point to whatever function. When you call it and if the arguments are wrong, the return is wrong, etc, you will get a compile error.
    C++ provides a lot of ways to avoid casts, as you can see.

    Quote Originally Posted by C_ntua View Post
    Polymorphism can be achieve with casting and virtual functions. Polymorphism is really important so making one of the two ugly doesn't seem as a good idea.
    Polymorphism is about not using casts.

    Quote Originally Posted by C_ntua View Post
    Pointers are not safe either, but C++ chose to have them. The point is that I would like for a language to provide a nice way to do things the way you want to. It is ok to provide safety for something that somebody can use without knowing what he is doing. But for something that you are aware of, like the effects of casting, it should be easy to use.
    As I understand it, pointers were kept for backwards compatibility. Originally, Bjarne wanted only references in the language.
    But I do agree with you - if the programmer is absolutely sure something must be done in a specific way, then the language should provide the tools to do so - after all, that is what C and C++ is all about - flexibility and speed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #49
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    As I understand it, pointers were kept for backwards compatibility. Originally, Bjarne wanted only references in the language.
    Not really. References were created pretty late in the game - when operator overloading was added to the language. And I don't think Bjarne ever considered extending them so that pointers would become unnecessary. That would make them the pointers instead.
    C++ has always been a hybrid language. A hybrid of procedural and object-oriented (and later generic) programming, and a hybrid of low-level system and high-level application programming. Pointers are an absolutely essential part of the low-level aspects of the language.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #50
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    Not really. References were created pretty late in the game - when operator overloading was added to the language. And I don't think Bjarne ever considered extending them so that pointers would become unnecessary. That would make them the pointers instead.
    C++ has always been a hybrid language. A hybrid of procedural and object-oriented (and later generic) programming, and a hybrid of low-level system and high-level application programming. Pointers are an absolutely essential part of the low-level aspects of the language.
    Can you actually solve problems such as linked lists with C++ type references [without an awful lot of workarounds]. As far as I understand, you can't re-assign a reference, so whilst you could set the next-element when you create a node, you couldn't then change it.

    --
    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.

  6. #51
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    My recommendation would be to cast less.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  7. #52
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Someone said that you have to caste in Java every second line, and I asked why, among other things. I would like to ask again. Why is casting in Java/C# OK, but not in C++. Elaborate a bit...

  8. #53
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    C++ casts are for C++ code. I didn't read the initial post or any susbsequent arguments. That is all there is to say about that and there is zero room for argument. Any argument is simply promoting being sloppy. They didn't make all "them fancy C++ casts" for no reason.

  9. #54
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by C_ntua View Post
    Someone said that you have to caste in Java every second line, and I asked why, among other things. I would like to ask again. Why is casting in Java/C# OK, but not in C++. Elaborate a bit...
    It's pretty much a relic from before Java had generics. When every collection can only store references to Object, you have to cast every single time you retrieve an element from the collection.

    Since much real-world code is about manipulating data in containers, there was a lot of casting.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #55
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by C_ntua View Post
    Someone said that you have to caste in Java every second line, and I asked why, among other things. I would like to ask again. Why is casting in Java/C# OK, but not in C++. Elaborate a bit...
    Well I'd say it's not OK in those languages either, but in too many cases there's just no other option because of their short sighted design.
    "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

  11. #56
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Quote Originally Posted by master5001 View Post
    C++ casts are for C++ code. I didn't read the initial post or any susbsequent arguments. That is all there is to say about that and there is zero room for argument. Any argument is simply promoting being sloppy. They didn't make all "them fancy C++ casts" for no reason.
    You realize that reading the entire topic helps, eh, buddy?
    It is not so much about sloppiness, but about ugliness and too much typing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #57
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by cpjust View Post
    Well I'd say it's not OK in those languages either, but in too many cases there's just no other option because of their short sighted design.
    What do you mean short sighted design? Java provides the same level of polymorphism with C++. If you can use a virtual function in C++ you can do in Java. If you can cast in Java you can do it in C++. C++ offers you safer casts, but you would cast in Java but not in C++?

    I don't defend anything, the answer that what you do in Java is worse than in C++ is acceptable. What I want to clarify is that if this is the case, and the designers of C++, Java, C# had different opinions about casting, how can you say it is "bad"?

    edit: By NO means I don't want to start a Java vs C++ topic. I just use them as examples as popular languages

  13. #58
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Quote Originally Posted by C_ntua View Post
    What do you mean short sighted design? Java provides the same level of polymorphism with C++. If you can use a virtual function in C++ you can do in Java. If you can cast in Java you can do it in C++. C++ offers you safer casts, but you would cast in Java but not in C++?
    Java and C++ are similar, but not the same language, thus what exists in C++ does not always exist in Java and vice versa. Remember that.
    And short-sighted design means that a language means you do in an X way, but sometimes, if you need to do it an Y way, then it is a short-sighted design, if the language hinders you from doing it the X way.

    I don't defend anything, the answer that what you do in Java is worse than in C++ is acceptable. What I want to clarify is that if this is the case, and the designers of C++, Java, C# had different opinions about casting, how can you say it is "bad"?
    Because they are not the same languages, and have different goals and strengths.
    For example, C++ is a type-strict language and thus circumventing this is said to be bad, and that is what casts do.
    But in C, for example, it is not bad to cast - indeed, it is preferred and a must in many situations because C is not type-strict.
    And I remember someone mentioning that casting is much a relic from old times in Java from when it lacked generics.

    As for specific language examples, I'm afraid that's beyond my knowledge.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #59
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    As far as type theory goes, Java is much more a type-safe language than C++.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #60
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Yeah, I forgot that you cannot cast whatever you want to whatever you want in Java. So in that way it is more type safe. On the other hand C++ is not type safe so you I guess you need to be sure, or more sure, that the casting is correct.

    As for the type-strict language, isn't also C, Java and C# type strict? You cannot assign something to something else, ever. Except if you cast or do something like int = float (which is another story).

    Personally I would still prefer shorter names, but it makes sense not to have them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reinterpret Cast to char* for Serialization?
    By 691175002 in forum C++ Programming
    Replies: 3
    Last Post: 02-01-2008, 07:44 PM
  2. Including The Right DLLs
    By bumfluff in forum Game Programming
    Replies: 8
    Last Post: 12-28-2006, 03:32 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Converting Double to Float
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2006, 02:46 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM