Should I avoid casting between data types?
I haven't really seen much about the use of this, but it seems like one of those last-minute-no-way-around kind of methods... but, for example, how would I use the length of a string in a for loop? The length property of a string is size_t, and if I had an integer i in a loop, how could I do that?
Where would casting be useful? Where should I avoid it? What are other methods of dealing with different data types that are better than cast?
winrawr
9
Junior Poster
Recommended Answers
Jump to PostFirst, the compiler will provide a warning (not error. Warnings are not fatal) for not casting between int and size_t... but there is no harm in doing either
for (int i=0; i <(int)somestring.length(); i++)
orfor (size_t i=0; i < somestring.length(); i++) {
. There is nothing wrong with casting... …
All 4 Replies
Comatose
290
Taboo Programmer
Team Colleague
grumpier
149
Posting Whiz in Training
winrawr
9
Junior Poster
grumpier
149
Posting Whiz in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.