What is the difference between the following two code segments?


// code segment 1
int i = 48;
char ch = i;

// code segment 2
int i = 48;
char ch = static_cast<char>(i);
First code converts the int to a char.
Second code casts int to a char. Think of a static_cast as being a more explicit conversion; sometimes it's necessary or the code won't compile (e.g. for enum classes).

In this case, the result is the same.
Last edited on
Topic archived. No new replies allowed.