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...
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...
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.
Too much to bring up. You can read a lot of the Java vs C++ replies, if you want. There are a few.
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.
Polymorphism is about not using casts.
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.
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.As I understand it, pointers were kept for backwards compatibility. Originally, Bjarne wanted only references in the language.
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
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.
My recommendation would be to cast less.
★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★
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...
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.
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
"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
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
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.
Because they are not the same languages, and have different goals and strengths.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"?
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.
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
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.