车立方" type="application/atom+xml">

hahakubile Blog, Powered by 车立方

Welcome to hahakubile's blog, You should know him. Thanks to 车立方

About Static_cast

static_cast

static_cast<type> (object);

The static_cast operator can be used for operations such as:

  • Converting a pointer of a base class to a pointer of a derived class,
  • Convert numeric data types such as enums to ints or ints to floats.

示例

int score = 1340;

float scale = static_cast<float>(score) / MAX_SCORE + 1;

// 如果这样呢?会有什么不同
float scale = score / MAX_SCORE + 1;

需要注意的是,在基础类型转换时,并非进行四舍五入,而是直接舍去小数部分!