Recently, I am confused with this two expression – (type)value and type(value).
For example : size_t c = size_t(-1); size_t c = (size_t)-1;
what is the difference?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The former is C++, it’s doing direct initialization by calling a constructor.
The second is C (or C++), it’s doing a cast.
So, there’s a lot of difference from a C programmer’s point of view.
Also, this is a pretty bad idea, since
size_tis an unsigned type. This should be done usingssize_t, which is signed and removes the need for the cast.