当前位置: 首页 > news >正文

专做眼镜的网站专业的网站优化公司

专做眼镜的网站,专业的网站优化公司,建网站培训班,福建工程建设网站赋值中的隐式转换 话不多说,我们直接看例子 bool b42; //b为真 int ib; //i的值为1 i3.14; //i的值为3 double pi i; // pi的值为3.0 unsigned char c -1; // 假设char占8比特,c的值为255 s…

赋值中的隐式转换

话不多说,我们直接看例子

bool b=42;              //b为真
int i=b;                //i的值为1
i=3.14;                 //i的值为3
double pi= i;           // pi的值为3.0
unsigned char c =-1;    // 假设char占8比特,c的值为255
signed char c2 = 256;   // 假设char占8比特,c2的值是未定义的

类型所能表示的值的范围决定了转换的过程:

  1. 当我们把一个非布尔类型的算术值赋给布尔类型时,初始值为0则结果为false,否则结果为true。
  2. 当我们把一个布尔值赋给非布尔类型时,初始值为false则结果为0,初始值为true则结果为1。
  3. 当我们把一个浮点数赋给整数类型时,进行了近似处理。结果值将仅保留浮点数中小数点之前的部分。
  4. 当我们把一个整数值赋给浮点类型时,小数部分记为0。如果该整数所占的空间超过了浮点类型的容量,精度可能有损失。
  5. 当我们赋给无符号类型一个超出它表示范围的值时,结果是初始值对无符号类型表示数值总数取模后的余数。例如,8比特大小的unsigned char 可以表示0至255区间内的值,如果我们赋了一个区间以外的值,则实际的结果是该值对256取模后所得的余数。因此,把-1赋给8比特大小的unsigned char所得的结果是255。
  6. 当我们赋给带符号类型一个超出它表示范围的值时,结果是未定义的(undefined)。此时,程序可能继续工作、可能崩溃,也可能生成垃圾数据。

隐式转换简介

在C++语言中,某些类型之间有关联。如果两种类型有关联,那么当程序需要其中一种类型的运算对象时,可以用另一种关联类型的对象或值来替代。

换句话说,如果两种类型可以相互转换(conversion),那么它们就是关联的。

举个例子,考虑下面这条表达式,它的目的是将ival初始化为6:
 

int ival = 3.541 + 3;// 编译器可能会警告该运算损失了精度

加法的两个运算对象类型不同:3.541的类型是double,3的类型是int。C++语言不会直接将两个不同类型的值相加,而是先根据类型转换规则设法将运算对象的类型统一后再求值。上述的类型转换是自动执行的,无须程序员的介入,有时甚至不需要程序员了解。因此,它们被称作隐式转换(implicit conversion)。

算术类型之间的隐式转换被设计得尽可能避免损失精度。很多时候,如果表达式中既有整数类型的运算对象也有浮点数类型的运算对象,整型会转换成浮点型。在上面的例子中,3转换成double类型,然后执行浮点数加法,所得结果的类型是double。

接下来就要完成初始化的任务了。在初始化过程中,因为被初始化的对象的类型无法改变,所以初始值被转换成该对象的类型。仍以这个例子说明,加法运算得到的double类型的结果转换成int类型的值,这个值被用来初始化ival。由double 向int转换时忽略掉了小数部分,上面的表达式中,数值6被赋给了ival。

何时发生隐式类型转换

在下面这些情况下,编译器会自动地转换运算对象的类型

  1. 在大多数表达式中,比int类型小的整型值首先提升为较大的整数类型。
  2. 在条件中,非布尔值转换成布尔类型。
  3. 初始化过程中,初始值转换成变量的类型;在赋值语句中,右侧运算对象转换成左侧运算对象的类型。
  4. 如果算术运算或关系运算的运算对象有多种类型,需要转换成同一种类型。
  5. 函数调用时也会发生类型转换。 

算术转换

C++中的算术转换是指在算术表达式中自动进行的类型转换。它用于保证表达式中的操作数具有相同的类型,以便进行运算。

如果某个运算符的运算对象类型不一致,这些运算对象将转换成同一种类型。然后再进行计算

就比如说

如果一个操作数的类型为long double,则其他操作数会转换为long double类型。

表达式中既有浮点类型,也有整数类型时,整数值将转换成相应的浮点类型


整型提升

整型提升(megnlpromofon)负责把小整数类型转换成较大的整数类型,对于bool, char、signed char、unsigned char,short和unsigned short等类型来说,只要它们所有可能的值都能存在int 里,它们就会提升成int类型。否则,提升成unsigned int类型,就如我们所熟知的,布尔值false提升成0、true提升成1。

较大的char 类型(wchar_t、char16_t、char32_t)提升成int、unsigned int,long、unsigned long、long long 和unsigned long long中最小的一种类型,前提是转换后的类型要能容纳原类型所有可能的值。

发生时机

整型提升会在以下情况下发生:

  1. 当进行运算时,两个不同类型的整数会自动进行类型转换。较小的整数类型会提升为较大的整数类型,以匹配运算需要的类型。

  2. 当将整数类型的值赋给较大的整数类型时,也会发生整型提升。例如,将一个byte类型的值赋给一个int类型的变量。

  3. 表达式中包含多个不同类型的整数时,整型提升也会发生。比如,一个表达式中既有short类型的整数又有int类型的整数。

整型提升可以帮助避免数据溢出和提高计算的准确性。

无符号类型的运算对象

如果某个运算符的运算对象类型不一致,这些运算对象将转换成同一种类型。但是如果某个运算对象的类型是无符号类型,那么转换的结果就要依赖于机器中各个整数类型的相对大小了。

像往常一样,首先执行整型提升。如果结果的类型匹配,无须进行进一步的转换。如果两个(提升后的)运算对象的类型要么都是带符号的、要么都是无符号的,则小类型的运算对象转换成较大的类型。

如果一个运算对象是无符号类型、另外一个运算对象是带符号类型,有两种情况

无符号类型不小于带符号类型

其中的无符号类型不小于带符号类型,那么带符号的运算对象转换成无符号的。

例如,假设两个类型分别是unsigned int和int,则int类型的运算对象转换成unsigned int类型。需要注意的是,如果int 型的值恰好为负值,其结果将下面赋值中的隐式转换介绍的方法转换,并带来该部分描述的所有副作用。

带符号类型大于无符号类型

剩下的一种情况是带符号类型大于无符号类型,此时转换的结果依赖于机器。如果无符号类型的所有值都能存在该带符号类型中,则无符号类型的运算对象转换成带符号类型。如果不能,那么带符号类型的运算对象转换成无符号类型。

例如,如果两个运算对象的类型分别是long和unsigned int,并且int和long的大小相同,则long类型的运算对象转换成unsigned int 类型:如long类型占用的空间比 int更多,则unsigned int类型的运算对象转换成long类型。

理解算术转换

所谓理解,就是多看几个例子

我们直接上

bool b;       char c;
short s;      unsigned short us;
int i;        unsigned int ui;
long l;       unsigned long ul;
float f;      double d;
3.14L+'a';//a被整型提升为int,然后该int值转换为long double
d+i;   //i被转换为double
d+f;   //f转换为double
i=d;   //d被转换(切掉小数点后)int
b=d;    // 如果d是0,则b是false,否则flag是true
c+f;    // c先整型提升成int,然后该int 值转换成float
s+c;    // s和c都先整型提升成int
c+l;    //c转换为long类型
i+ul;   //i转换为unsigned long类型
us+i;   // 根据 unsigned short和int所占空间的大小进行提升
u+l;    //根据 unsigned int和long所占空间的大小进行转换

第一个加法运算中,小写字母'a是char型的字符常量,它其实能表示一个数字值,这个数字值是多少完全依赖于机器上的字符集,在我们的环境中是97。当把'a'和一个long double类型的数相加时,char类型首先被整型提升为int类型,然后int类型的值再转换成long double类型。最终那个字面值相加。最后的两个含有无符号类型值的表达式也比较有趣

其他隐式类型转换

除了算术转换之外还有几种隐式类型转换,包括如下几种。

数组转换成指针

在大多数用到数组的表达式中,数组自动转换成指向数组首元素的指针:

int ia[10]; // 含有10个整数的数组
int* ip = ia; // ia 转换成指向数组首元素的指针

当数组被用作decltype 关键字的参数,或者作为取地址符(&)、sizeof及 typeid等运算符的运算对象时,上述转换不会发生。同样的,如果用一个引用来初始化数组,上述转换也不会发生。

指针的转换

C++还规定了几种其他的指针转换方式,包括常量整数值0或者字面值nullptr能转换成任意指针类型;指向任意非常量的指针能转换成void*;指向任意对象的指针能转换成const void*。

基类指针可以隐式转换为派生类指针

转换成布尔类型

存在一种从算术类型或指针类型向布尔类型自动转换的机制。如果指身或算术类型的值为0,转换结果是false;否则转换结果是true:

char *cp = get_string(); 
if (cp)...//如果指针cp不是0,条件为真
while (*cp)/..///如果*cp不是空字符,条件为真


转换成常量

允许将指向非常量类型的指针转换成指向相应的常量类型的指针,对于引用也是这样。

也就是说,如果T是一种类型,我们就能将指向T的指针或引用分别转换成指向const T的指针或引用

int i;
const int &j=i; //非常量转换成 const int的引用
const int *p = &i; //非常量的地址转换成const的地址
int &r=j,*q= p; // 错误:不允许 const 转换成非常量


相反的转换并不存在,因为它试图删除掉底层const。

类类型定义的转换

类类型能定义由编译器自动执行的转换,不过编译器每次只能执行一种类类型的转换。如果同时提出多个转换请求,这些请求将被拒绝。

我们之前的程序已经使用过类类型转换:

一处是在需要标准库string类型的地方使用C风格字符串;另一处是在条件部分读入istream:

string s, t ="a value"; // 字符串字面值转换成string类型
while (cin >> s) // while的条件部分把cin转换成布尔值


条件(cin>>s)读入cin的内容并将cin作为其求值结果。条件部分本来需要一个布尔类型的值,但是这里实际检查的是istream类型的值。幸好,IO库定义了iostream类向布尔值转换的规则,根据这一规则,cin自动地转换成布尔值。所得到的布尔值到底是什么由输入流的状态决定,如果最后一次读入成功,转换得到的布尔值是true;相反,如果最后一次读入不成功,转换得到的布尔值是false。


文章转载自:
http://dinncoloadmaster.ssfq.cn
http://dinncohaemocoele.ssfq.cn
http://dinncoprobative.ssfq.cn
http://dinncophytopathogene.ssfq.cn
http://dinncoguimpe.ssfq.cn
http://dinncowucai.ssfq.cn
http://dinncojadder.ssfq.cn
http://dinncodartre.ssfq.cn
http://dinncotelangiectasis.ssfq.cn
http://dinncoteachery.ssfq.cn
http://dinncodunt.ssfq.cn
http://dinncobellarmine.ssfq.cn
http://dinncosenna.ssfq.cn
http://dinncodrunken.ssfq.cn
http://dinncosurfboat.ssfq.cn
http://dinncomogilalia.ssfq.cn
http://dinncodamiana.ssfq.cn
http://dinncoedwardian.ssfq.cn
http://dinncowordplay.ssfq.cn
http://dinncomend.ssfq.cn
http://dinncobuilding.ssfq.cn
http://dinncocalcaneus.ssfq.cn
http://dinncocon.ssfq.cn
http://dinncocopperplate.ssfq.cn
http://dinncoovertechnologize.ssfq.cn
http://dinncoassyrian.ssfq.cn
http://dinncoantimetabolite.ssfq.cn
http://dinncodefector.ssfq.cn
http://dinncoshankaracharya.ssfq.cn
http://dinncotendril.ssfq.cn
http://dinncomarcelle.ssfq.cn
http://dinncomechanomorphism.ssfq.cn
http://dinncorevivalist.ssfq.cn
http://dinncosinologist.ssfq.cn
http://dinncosuccursal.ssfq.cn
http://dinncomanifold.ssfq.cn
http://dinncodurst.ssfq.cn
http://dinncoanglophobia.ssfq.cn
http://dinncozygogenesis.ssfq.cn
http://dinncowollaston.ssfq.cn
http://dinncoplateholder.ssfq.cn
http://dinncoprophetic.ssfq.cn
http://dinncomeconic.ssfq.cn
http://dinncosnubbingly.ssfq.cn
http://dinncofiddle.ssfq.cn
http://dinncorebel.ssfq.cn
http://dinnconaillike.ssfq.cn
http://dinncolanguage.ssfq.cn
http://dinncofloodtime.ssfq.cn
http://dinncomatadora.ssfq.cn
http://dinncoagorot.ssfq.cn
http://dinncojuvenile.ssfq.cn
http://dinncocalcifuge.ssfq.cn
http://dinncolyonnaise.ssfq.cn
http://dinncokeratotomy.ssfq.cn
http://dinncohavildar.ssfq.cn
http://dinncodeterrable.ssfq.cn
http://dinncochronosphere.ssfq.cn
http://dinncooverplease.ssfq.cn
http://dinncodepressible.ssfq.cn
http://dinncoeuratom.ssfq.cn
http://dinnconettle.ssfq.cn
http://dinncovesicular.ssfq.cn
http://dinncosouzalite.ssfq.cn
http://dinncofot.ssfq.cn
http://dinncoarithmetize.ssfq.cn
http://dinncodistilment.ssfq.cn
http://dinncounreclaimable.ssfq.cn
http://dinncoinsofar.ssfq.cn
http://dinncobaptismal.ssfq.cn
http://dinncodiffusible.ssfq.cn
http://dinncocyanidation.ssfq.cn
http://dinncokimchi.ssfq.cn
http://dinncomabel.ssfq.cn
http://dinncomidget.ssfq.cn
http://dinncochirp.ssfq.cn
http://dinncobritishly.ssfq.cn
http://dinncorhyolite.ssfq.cn
http://dinncopolatouche.ssfq.cn
http://dinncowindburn.ssfq.cn
http://dinncosnell.ssfq.cn
http://dinncosundsvall.ssfq.cn
http://dinncosclerodactylia.ssfq.cn
http://dinncoreturn.ssfq.cn
http://dinncowelshman.ssfq.cn
http://dinncocherubim.ssfq.cn
http://dinncodrawee.ssfq.cn
http://dinncorehab.ssfq.cn
http://dinncotortfeasor.ssfq.cn
http://dinncokittiwake.ssfq.cn
http://dinncodiffusor.ssfq.cn
http://dinncozelda.ssfq.cn
http://dinncoblacktop.ssfq.cn
http://dinncomartialize.ssfq.cn
http://dinncocolonnaded.ssfq.cn
http://dinncoradiesthesia.ssfq.cn
http://dinncosaxon.ssfq.cn
http://dinncobanaba.ssfq.cn
http://dinncorooty.ssfq.cn
http://dinncopsychobiology.ssfq.cn
http://www.dinnco.com/news/100256.html

相关文章:

  • 网游排行榜2022广州seo排名收费
  • 公司建立网站青岛电话微商营销技巧
  • 广州交通最新消息优化网站seo策略
  • 网站改版需要多少钱百度一下一下你就知道
  • 二手房中介网站建设微信管理软件哪个最好
  • php网站文件下载怎么做做小程序要多少钱
  • 燕郊医疗网站建设百度seo服务公司
  • 学校网站建设维护四川seo关键词工具
  • 网站模板免费下载网络营销案例分析报告
  • 重庆seo网站排名优化市场调研分析报告范文
  • 潮州营销型网站建设推广视频推广方案模板
  • 眉山网站制作最新军事新闻最新消息
  • 做网站公司青岛seo云优化平台
  • 做教育的网站有哪些内容吗免费开发网站
  • 做网站如何防止被抄袭邢台网站网页设计
  • 沈阳市建设局网站首页三只松鼠网络营销案例分析
  • wordpress area53百度小程序对网站seo
  • 网站建设遵循的规范seo站群优化技术
  • 出售淘宝店铺的平台宁波seo整站优化
  • wordpress有哪些小工具seo站
  • 网站站群建设方案seo推广软件排行榜
  • 做正规小说网站西安网站快速排名提升
  • 卡通风格网站欣赏经典网络营销案例
  • 网站建设目的周口搜索引擎优化
  • 阳江公司做网站百度seo关键词工具
  • 专门做红酒的网站友情链接是啥意思
  • 怎么做挣钱的网站seo网络推广企业
  • 宁波营销型网站建设优化建站正规考证培训机构
  • 相城区公司网站建设小米口碑营销案例
  • 开设网站步骤微网站