Why use static_cast(x) instead of (int)x?

前端 未结 9 1895
滥情空心
滥情空心 2020-11-22 02:38

I\'ve heard that the static_cast function should be preferred to C-style or simple function-style casting. Is this true? Why?

9条回答
  •  独厮守ぢ
    2020-11-22 02:45

    static_cast, aside from manipulating pointers to classes, can also be used to perform conversions explicitly defined in classes, as well as to perform standard conversions between fundamental types:

    double d = 3.14159265;
    int    i = static_cast(d);
    

提交回复
热议问题