Hi all,
I have recently started to teach myself to program with C++ and have a question concerning casts.
What is the difference between -
i = sqrt(static_cast<double>(n))
and -
i = sqrt((double) n)
Many thanks,
Rob
Hi all,
I have recently started to teach myself to program with C++ and have a question concerning casts.
What is the difference between -
i = sqrt(static_cast<double>(n))
and -
i = sqrt((double) n)
Many thanks,
Rob
Google knows all
First hit should give you your answer.
Sorry, I still don't understand. Can someone spell it out for me?
They are, in this particular use, the same. One is C++ style, the other is C style, but the result is the same.
However, the C++ style cast family is more specific than the C style casts, and this allows better checking of various conditions where in C you can do really stupid things, in C++ the compiler tells you "that's not right".
It is also easier (if you understand the C++ style casts) to see what is actually being done and whether it is (for example) a portable cast, or if it's going to potentially cause problems when porting to another architecture. For example
doesn't really tell you much of what is done with x.Code:char * p = (char *)x;
On the other hand
is more clearly saying "we have some other type than char * in x". And it's very easy to search for reinterpret_cast, whilst a (char *) cast is quite dificult to find - particularly you want to find the other casts that translate to (int *) or (mystruct *).Code:char *p = reinterpret_cats<char *>(x);
--
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.
Aha! I suspected as much. I tried substituting each type in a program and they seemed to do the same thing. Many thanks for your patience and the explanation.