c++几种类型转换方式

 

1、初始化和赋值进行的转换:

1 float one=1;//整型转浮点型
2 
3 int guess(2.333);//double转int
4 
5 int debt=7.2e12;//flot转int ,但是无法储存过大数据;

2、以{}方式初始化时进行转换

1 //条件:不允许转换最大范围还大大的数据,比如float就不能转换到int,但是int在小于256的时候还是可以转换成char,而且初始化转换的数据必须是常量
2 char c={66};
3 const c1=66;
4 char c={c1};

3、表达式中的转换

1 short one=1;
2 short two=2;
3 short three=one+two;
4 
5 //在第三行的时候,C++程序将one和two的值转换成int,然后再将结果转换成short因为计算机通常选择int作为最自然的类型.

4、传参时转换

5、强制转换

1 (long)sample //C
2 long (sample) //C++
3 static_cast<long>(sample)  //C++ 比传统转换更为严格

6、c++中auto的声明

1 auto one=1;//int
2 auto two=2.0;//double
3 auto n=1.3e12L//long double


 

 

posted @ 2014-02-27 21:12  随心随想  阅读(662)  评论(0编辑  收藏  举报