C++ϵͳѧϰ֮Î壺±í´ïʽ

2018-08-10 11:12:06À´Ô´£º²©¿ÍÔ° ÔĶÁ ()

ÐÂÀÏ¿Í»§´ó»ØÀ¡,ÔÆ·þÎñÆ÷µÍÖÁ5ÕÛ

¡¡¡¡±í´ïʽÓÉÒ»¸ö»ò¶à¸öÔËËã¶ÔÏó×é³É£¬¶Ô±í´ïʽÇóÖµ½«µÃµ½Ò»¸ö½á¹û¡£×ÖÃæÖµºÍ±äÁ¿ÊÇ×î¼òµ¥µÄ±í´ïʽ£¬Æä½á¹û¾ÍÊÇ×ÖÃæÖµºÍ±äÁ¿µÄÖµ¡£°ÑÒ»¸öÔËËã·ûºÍÒ»¸ö»ò¶à¸öÔËËã¶ÔÏó×éºÏÆðÀ´¿ÉÒÔÉú³É½Ï¸´Ôӵıí´ïʽ¡£

»ù´¡

1¡¢»ù±¾¸ÅÄî

  • Ò»ÔªÔËËã·û
  • ¶þÔªÔËËã·û
  • ÈýÔªÔËËã·û

 ×óÖµºÍÓÒÖµ

¡¡¡¡C++µÄ±í´ïʽҪôÊÇ×óÖµ£¬ÒªÃ´¾ÍÊÇÓÒÖµ¡£

¡¡¡¡µ±Ò»¸ö¶ÔÏó±»ÓÃ×÷ÓÒÖµµÄʱºò£¬ÓõÄÊǶÔÏóµÄÖµ£¨ÄÚÈÝ£©£»µ±¶ÔÏó±»ÓÃ×÷×óÖµµÄʱºò£¬ÓõÄÊǶÔÏóµÄÉí·Ý£¨ÔÚÄÚ´æÖеÄλÖã©¡£

¡¡¡¡ÔÚÐèÒªÓÒÖµµÄµØ·½¿ÉÒÔÓÃ×óÖµÀ´´úÌ棬µ«ÊDz»ÄÜ°ÑÓÒÖµµ±³É×óֵʹÓá£

2¡¢ÓÅÏȼ¶Óë½áºÏÂÉ

¡¡¡¡¸´ºÏ±í´ïʽÊÇÖ¸º¬ÓÐÁ½¸ö»ò¶à¸öÔËËã·ûµÄ±í´ïʽ¡£Ç󸴺ϱí´ïʽµÄÖµÐèÒªÊ×ÏȽ«ÔËËã·ûºÍÔËËã¶ÔÏóºÏÀíµØ×éºÏÔÚÒ»Æð£¬ÓÅÏȼ¶Óë½áºÏÂɾö¶¨ÁËÔËËã¶ÔÏó×éºÏµÄ·½Ê½¡£

À¨ºÅÎÞÊÓÓÅÏȼ¶Óë½áºÏÂÉ

3¡¢Çóֵ˳Ðò

ÓÅÏȼ¶¹æ¶¨ÁËÔËËã¶ÔÏóµÄ×éºÏ·½Ê½£¬µ«ÊÇûÓÐ˵Ã÷ÔËËã¶ÔÏó°´ÕÕʲô˳ÐòÇóÖµ¡£ÔÚ´ó¶àÊýÇé¿öÏ£¬²»»áÃ÷È·Ö¸¶¨ÇóÖµµÄ˳Ðò¡£

int i=f1()*f2();

ÉÏÊö¾ÍÎÞ·¨ÖªµÀµ½µ×f1ÔÚf2֮ǰµ÷Ó㬻¹ÊÇf2ÔÚf1֮ǰµ÷Óá£

NOTE£º¶ÔÓÚÄÇЩûÓÐÖ¸¶¨Ö´ÐÐ˳ÐòµÄÔËËã·ûÀ´Ëµ£¬Èç¹û±í´ïʽָÏò²¢ÐÞ¸ÄÁËͬһ¸ö¶ÔÏ󣬽«»áÒý·¢´íÎó²¢²úÉú䶨ÒåµÄÐÐΪ¡£

int i=0;
cout<<i<<"  "<<++i<<endl;    //䶨Òå

£¨µ«ÊÇVS2013ÖвâÊÔ½á¹ûÊÇÏȽøÐÐ++iÔËË㣩

 

ÓÐËÄÖÖÔËËã·ûÃ÷È·¹æ¶¨ÁËÔËËã¶ÔÏóµÄÇóֵ˳Ðò¡£

  • Âß¼­Ó루&&£©ÔËËã·û£ºÏÈÇó×ó²à¶ÔÏóµÄÖµ£¬Ö»Óе±×ó²àÔËËã¶ÔÏóµÄֵΪÕæʱ²Å¼ÌÐøÇóÓÒ²à¶ÔÏóµÄÖµ
  • Âß¼­»ò£¨||£©ÔËËã·û
  • Ìõ¼þ£¨£¿£º£©ÔËËã·û
  • ¶ººÅÔËËã·û

Çóֵ˳Ðò¡¢ÓÅÏȼ¶¡¢½áºÏÂÉ

ÔËËã¶ÔÏóµÄÇóֵ˳ÐòÓëÓÅÏȼ¶ºÍ½áºÏÂÉÎ޹ء£µ±±í´ïʽÖжÔÏ󲻸ıäͬһ¶ÔÏóʱ£¬ÆäÇóֵ˳Ðò²»ÊÜÏÞÖÆ£¬Èç¹ûÓжÔͬһ¶ÔÏó²úÉúÓ°Ï죬Ôò»áÊÇÒ»Ìõ´íÎóµÄ±í´ïʽ£¬²úÉú䶨ÒåµÄÐÐΪ¡££¨VS13ÖÐÒѾ­²»±¨´í£¬¶øÊÇ°´ÕÕ´Ó×óµ½ÓÒµÄ˳ÐòÇóÖµ£¬Èç¹û±í´ïʽÖÐÖ»ÓÐÒ»¸ö¶ÔÏó¸Ä±äÆäÖµ£¬ÔòÏÈÇó¸Ä±ä¶ÔÏóµÄÖµ£©

int n = 0;

int f1()
{
	return ++n;
}

int f2()
{
	return --n;
}

int main()
{
	int a = f1() + f2();
	cout << a << endl;
	system("pause");
	return 0;
}

Êä³ö½á¹ûÊÇ1.

ÒòΪf1ºÍf2¶¼¶ÔnÓиı䣬ËùÒÔÏÈÇóf1µÄÖµÔÙÇóf2µÄÖµ¡£

int i = 0;
cout << i << "  " << ++i << endl;

Êä³ö½á¹ûÊÇ1,1.

ÒòΪ++i»á¸Ä±ä¶ÔÏó£¬ËùÒÔÏȶÔÆäÇóÖµ£¬ÔÙ´Ó×óµ½ÓҵĽáºÏÂÉÖ´ÐС£

ËãÊõÔËËã·û

ËãÊõÔËËã·ûµÄÔËËã¶ÔÏóºÍÇóÖµ½á¹û¶¼ÊÇÓÒÖµ¡£ÔÚ±í´ïʽÇóֵ֮ǰ£¬Ð¡ÕûÊýÀàÐ͵ÄÔËËã¶ÔÏó±»ÌáÉý³É½Ï´óµÄÕûÊýÀàÐÍ£¨int£©£¬ËùÓÐÔËËã¶ÔÏó×îÖÕ»áת»»³ÉͬһÀàÐÍ¡£

¶Ô´ó¶àÊýÔËËã·ûÀ´Ëµ£¬²¼¶ûÀàÐ͵ÄÔËËã¶ÔÏ󽫱»ÌáÉýΪint£¬¶ÔÓÚÒ»ÔªÔËËã·û+»ò-£¬²¼¶ûÖµ²»Ó¦¸Ã²ÎÓëÔËËã¡£

ÕûÊýÏà³ýµÄ½á¹û»¹ÊÇÕûÊý£¬Ð¡Êý²¿·ÖÖ±½ÓÆúµô¡£

%È¡Óࣺ¼ÆËãÁ½¸öÕûÊýÏà³ýËùµÃµÄÓàÊý£¬ÔËËã¶ÔÏó±ØÐ붼ÊÇÕûÊý¡£

 Âß¼­ºÍ¹ØϵÔËËã·û

¹ØϵÔËËã·û×÷ÓÃÓòËãÊõÀàÐÍ»òÖ¸ÕëÀàÐÍ£¬Âß¼­ÔËËã·û×÷ÓÃÓÚÈÎÒâÄÜת»»³É²¼¶ûÖµµÄÀàÐÍ¡£Âß¼­ÔËËã·ûºÍ¹ØϵÔËËã·ûµÄ·µ»ØÖµ¶¼ÊDz¼¶ûÀàÐÍ¡£¶þÕßµÄÔËËã¶ÔÏóºÍÇóÖµ½á¹û¶¼ÊÇÓÒÖµ¡£

Âß¼­ÓëºÍÂß¼­»òÔËËã·û

¶þÕ߶¼ÊÇÏÈÇó×ó²àÔËËã¶ÔÏóµÄÖµÔÙÇóÓÒ²àÔËËã¶ÔÏóµÄÖµ¡£

¶Ì·ÇóÖµ£ºµ±×ó²àÔËËã¶ÔÏóµÄÖµÎÞ·¨È·¶¨±í´ïʽµÄ½á¹ûʱ²Å»á¼ÆËãÓÒ²àÔËËã¶ÔÏóµÄÖµ¡£

¸³ÖµÔËËã·û

¸³ÖµÔËËã·ûµÄ×ó²àÔËËã¶ÔÏó±ØÐëÊÇÒ»¸ö¿ÉÐ޸ĵÄ×óÖµ¡£

¸³ÖµÔËËãµÄ½á¹ûÊÇËüµÄ×ó²àÔËËã¶ÔÏ󣬲¢ÇÒÊÇÒ»¸ö×óÖµ¡£ÏàÓ¦µÄ£¬½á¹ûµÄÀàÐ;ÍÊÇ×ó²àÔËËã¶ÔÏóµÄÀàÐÍ¡£Èç¹û¸³ÖµÔËËã·ûµÄ×óÓÒÁ½¸öÔËËã¶ÔÏóÀàÐͲ»Í¬£¬ÔòÓÒ²àÔËËã¶ÔÏóת»»³É×ó²àÔËËã¶ÔÏóµÄÀàÐÍ¡£

¸³ÖµÔËËãÂú×ãÓÒ½áºÏÂÉ

ÆäËû¶þÔªÔËËã·ûÂú×ã×ó½áºÏÂÉ£¬¸³ÖµÔËËã·µ»ØµÄÊÇÆä×ó²àÔËËã¶ÔÏó¡£

 ¸³ÖµÔËËãÓÅÏȼ¶½ÏµÍ

ͨ³£ÐèÒª¸ø¸³Öµ²¿·Ö¼ÓÉÏÀ¨ºÅʹÆä·ûºÏÎÒÃǵÄÔ­Òâ¡£

²»Òª»ìÓø³ÖµÔËËã·ûºÍÏàµÈÔËËã·û

¸´ºÏ¸³ÖµÔËËã·û

ÈÎÒâÒ»ÖÖ¸´ºÏÔËËã·û¶¼ÍêÈ«µÈ¼ÛÓÚ£º

a=a op b;

Çø±ðÔÚÓÚ×ó²àÔËËã¶ÔÏóµÄÇóÖµ´ÎÊý£ºÊ¹Óø´ºÏÔËËã·ûÖ»ÇóÖµÒ»´Î£¬Ê¹ÓÃÆÕͨÔËËã·ûÔòÇóÖµÁ½´Î¡£

µÝÔöºÍµÝ¼õÔËËã·û

µÝÔöºÍµÝ¼õÔËËã·ûÓÐÁ½ÖÖÐÎʽ£ºÇ°Öð汾ºÍºóÖð汾¡£

Ç°Öð汾ÊÇÏȶԶÔÏó½øÐеÝÔö»òµÝ¼õÔËËã²¢·µ»ØÔËËãÖ®ºóµÄ¶ÔÏóÖµ£¬¶ÔÏó±¾ÉíÒѾ­·¢Éú¸Ä±ä¡£

ºóÖð汾ÊÇÏÈ·µ»Øδ¸Ä±ä֮ǰµÄ¶ÔÏóµÄÖµ£¬È»ºóÔÙ¶Ô¶ÔÏó½øÐеÝÔö»òµÝ¼õ²Ù×÷¡£

NOTE£º³ý·Ç±ØÐ룬·ñÔò²»ÓõÝÔö»òµÝ¼õÔËËã·ûµÄºóÖð汾¡£

ÔÚÒ»ÌõÓï¾äÖлìÓýâÒýÓú͵ÝÔöÔËËã·û

Èç¹ûÏëÔÚÒ»Ìõ¸´ºÏ±í´ïʽÖмȽ«±äÁ¿¼Ó1»ò¼õ1ÓÖÄÜʹÓÃËüÔ­À´µÄÖµ£¬Õâʱ¾Í¿ÉÒÔʹÓõÝÔö»òµÝ¼õµÄºóÖð汾¡£

NOTE£ººóÖÃÔËËã·ûµÄÓÅÏȼ¶¸ßÓÚ½âÒýÓÃÔËËã·û£¬Òò´Ë*p++µÈ¼ÛÓÚ*(p++)

³ÉÔ±·ÃÎÊÔËËã·û

µãÔËËã·ûºÍ¼ýÍ·ÔËËã·û

NOTE£º½âÒýÓÃÔËËã·ûµÄÓÅÏȼ¶µÍÓÚµãÔËËã·û£¬ËùÒÔÖ´ÐнâÒýÓÃÔËËãµÄ×Ó±í´ïʽÁ½¶Ë±ØÐë¼ÓÉÏÀ¨ºÅ¡£

Ìõ¼þÔËËã·û

Ìõ¼þÔËËã·û£¨£¿£º£©ÔÊÐí°Ñ¼òµ¥µÄif-elseÂß¼­Ç¶Èëµ½µ¥¸ö±í´ïʽÖÐ

cond£¿expr1£ºexpr2

condÊÇÅжÏÌõ¼þµÄ±í´ïʽ£¬expr1ºÍexpr2ÊÇÁ½¸öÀàÐÍÏàͬ»ò¿ÉÄÜת»»ÎªÄ³¸ö¹«¹²ÀàÐ͵ıí´ïʽ¡£

±í´ïʽµÄ½á¹ûÊÇexpr1»òexpr2µÄ·µ»ØÖµ¡£µ±Á½¸ö±í´ïʽ¶¼ÊÇ×óÖµ»òÄÜת»»³ÉͬһÖÖ×óÖµÀàÐÍʱ£¬ÔËËãµÄ½á¹ûŶÊÇ×óÖµ£»·ñÔòÔËËãµÄ½á¹ûÊÇÓÒÖµ¡£

 Ç¶Ì×Ìõ¼þÔËËã·û

Ìõ¼þÔËËãµÄǶÌ××îºÃ±ð³¬¹ýÁ½µ½Èý²ã¡£

ÔÚÊä³ö±í´ïʽÖÐʹÓÃÌõ¼þÔËËã·û

 Ìõ¼þÔËËã·ûµÄÓÅÏȼ¶·Ç³£µÍ£¬Òò´Ëµ±Ò»Ìõ³¤±í´ïʽÖÐǶÌ×ÁËÌõ¼þÔËËã×Ó±í´ïʽʱ£¬Í¨³£ÐèÒª¼ÓÉÏÀ¨ºÅ¡£

λÔËËã·û

λÔËËã·û×÷ÓÃÓÚÕûÊýÀàÐ͵ÄÔËËã¶ÔÏ󣬲¢°ÑÔËËã¶ÔÏó¿´³ÉÊǶþ½øÖÆλµÄ¼¯ºÏ¡£Î»ÔËËã·ûÌṩ¼ì²éºÍÉèÖöþ½øÖÆλµÄ¹¦ÄÜ¡£

 

Ò»°ãÀ´Ëµ£¬Èç¹ûÔËË㸴ϰʱСÕûÐÍ£¬Ôò»á×Ô¶¯ÌáÉý³É½Ï´óµÄÕûÊýÀàÐÍ¡£ÔËËã¶ÔÏó¿ÉÒÔÊÇ´ø·ûºÅ£¬Ò²¿ÉÒÔÊÇÎÞ·ûºÅ¡£Èç¹ûÔËËã¶ÔÏóÊÇ´ø·ûºÅµÄÇÒËüµÄֵΪ¸º£¬ÄÇôλÔËËã·ûÈçºÎ´¦Àí¶ÔÏóµÄ¡°·ûºÅλ¡±ÒÀÀµÓÚ»úÆ÷£¬×óÒƲÙ×÷¿ÉÄܸıä·ûºÅλµÄÖµ£¬Òò´ËÊÇÒ»ÖÖ䶨ÒåµÄÐÐΪ¡£

NOTE£ºÇ¿ÁÒ½¨Òé½ö½«Î»ÔËËã·ûÓÃÓÚ´¦ÀíÎÞ·ûºÅÀàÐÍ¡£

ÒÆλÔËËã·û

Ê×ÏÈÁî×ó²àÔËËã¶ÔÏóµÄÄÚÈÝ°´ÕÕÓÒ²àÔËËã¶ÔÏóµÄÒªÇóÒƶ¯Ö¸¶¨Î»Êý£¬È»ºó½«¾­¹ýÒƶ¯µÄ×ó²àÔËËã¶ÔÏóµÄ¿½±´×÷ΪÇóÖµ½á¹û¡£

ÆäÖУ¬ÓÒ²àµÄÔËËã¶ÔÏóÒ»¶¨²»ÄÜΪ¸º£¬¶øÇÒÖµ±ØÐëÑϸñСÓÚ½á¹ûµÄλÊý£¬·ñÔò²úÉú䶨ÒåµÄÐÐΪ¡£¶þ½øÖÆλÒƳö±ß½çÖ®ÍâµÄλ±»ÉáÆúµô¡£

λÇó·´ÔËËã·û

λÓ롢λ»ò¡¢Î»Òì»òÔËËã·û

sizeofÔËËã·û

sizeofÔËËã·û·µ»ØÒ»Ìõ±í´ïʽ»òÒ»¸öÀàÐÍÃû×ÖËùÕ¼µÄ×Ö½ÚÊý¡£ÆäÖµÊÇÒ»¸ösize_tÀàÐ͵ij£Á¿±í´ïʽ¡£

sizeof²¢²»Êµ¼Ê¼ÆËãÆäÔËËã¶ÔÏóµÄÖµ¡£

  • ¶Ôchar»òÕßÀàÐÍΪcharµÄ±í´ïʽִÐÐsizeofÔËË㣬½á¹ûΪ1
  • ¶ÔÒýÓúͽâÒýÓÃÖ¸ÕëÖ´ÐÐsizeof£¬µÃµ½µÄÊÇʵ¼Ê¶ÔÏóËùÕ¼¿Õ¼äµÄ´óС
  • ¶ÔÖ¸ÕëµÄÔËËã½á¹ûÊÇÖ¸Õë±¾ÉíËùÕ¼¿Õ¼äµÄ´óС
  • ¶ÔÊý×éµÄÔËËã½á¹ûÊÇÕû¸öÊý×éËùÕ¼¿Õ¼äµÄ´óС£¬µÈ¼ÛÓÚ¶ÔÊý×éÖÐËùÓеÄÔªËظ÷Ö´ÐÐÒ»´ÎµÄ½á¹ûÖ®ºÍ
  • ¶ÔstringºÍvectorµÄÖ´Ðнá¹ûÊǸÃÀàÐ͹̶¨²¿·ÖµÄ´óС£¬²»»á¼ÆËã¶ÔÏóÖеÄÔªËØÕ¼ÓÃÁ˶àÉÙ¿Õ¼ä

NOTE£ºsizeofµÄ·µ»ØÖµÊdz£Á¿±í´ïʽ£¬Òò´Ë¿ÉÒÔÓÃÀ´¶¨ÒåÊý×é¡£

 ¶ººÅÔËËã·û

¶ººÅÔËËã·ûº¬ÓÐÁ½¸öÔËËã¶ÔÏ󣬰²×°´Ó×óÏòÓÒµÄ˳ÐòÒÀ´ÎÇóÖµ¡£

Ê×ÏȶÔ×ó²àµÄ±í´ïʽÇóÖµ£¬È»ºó½«ÇóÖµ½á¹û¶ªÆúµô£¬¶ººÅÔËËã·ûÕæÕýµÄ½á¹ûÊÇÓÒ²à±í´ïʽµÄÖµ¡£Èç¹ûÓÒ²à¶ÔÏóÊÇ×óÖµ£¬×îÖÕ½á¹ûÒ²ÊÇ×óÖµ¡£

¶ººÅÔËËã·û¾­³£±»ÓÃÔÚforÑ­»·ÖС£

ÀàÐÍת»»

ËãÊõÀàÐÍÖ®¼äµÄÒþʽת»»±»Éè¼ÆµÃ¾¡¿ÉÄܱÜÃâËðʧ¾«¶È¡£

ºÎʱ·¢ÉúÒþʽת»»

  • ´ó¶àÊý±í´ïʽÖУ¬±ÈintÀàÐÍСµÄÕûÐÍÖµÊ×ÏÈÌáÉýΪ½Ï´óµÄÕûÊýÀàÐÍ
  • ÔÚÌõ¼þÖУ¬·Ç²¼¶ûֵת»»³É²¼¶ûÀàÐÍ
  • ³õʼ»¯¹ý³ÌÖУ¬³õʼֵת»»³É±äÁ¿µÄÀàÐÍ£»ÔÚ¸³ÖµÓï¾äÖУ¬ÓÒ²àÔËËã¶ÔÏóת»»³É×ó²àÔËËã¶ÔÏóµÄÀàÐÍ
  • Èç¹ûËãÊõÔËËã»ò¹ØϵÔËËãµÄÔËËã¶ÔÏóÓжàÖÖÀàÐÍ£¬ÐèҪת»»³ÉͬһÖÖÀàÐÍ¡£
  • º¯Êýµ÷ÓÃʱҲ»á·¢ÉúÀàÐÍת»»

ËãÊõת»»

ÔËËã·ûµÄÔËËã¶ÔÏóת»»³É×î¿íµÄÀàÐÍ¡£µ±±í´ïʽÖмÈÓи¡µãÓÖÓÐÕûÐ͵Äʱºò£¬ÕûÐÍת»»Îª¸¡µã¡£

ÕûÐÍÌáÉý

  • СÕûÊýÀàÐÍ£¨bool¡¢char¡¢signed char¡¢unsigned char¡¢shortºÍunsigned short£©×ª»»³ÉintÀàÐÍ
  • ½Ï´óµÄcharÀàÐÍ£¨wchar_t¡¢char16_t¡¢char32_t£©ÌáÉýΪint¡¢unsigned¡¢long¡¢unsigned long¡¢long longºÍunsigned long longÖÐÄÜÈÝÄÉÆäÖµµÄ×îСµÄÒ»ÖÖ

ÎÞ·ûºÅÀàÐ͵ÄÔËËã¶ÔÏó

  • ÎÞ·ûºÅÀàÐÍ´óÓÚ´ø·ûºÅÀàÐÍ£¬Ôò´ø·ûºÅÀàÐÍת»»³ÉÎÞ·ûºÅÀàÐÍ
  • ÎÞ·ûºÅÀàÐÍСÓÚ´ø·ûºÅÀàÐÍ£¬×ª»»½á¹ûÒÀÀµÓÚ»úÆ÷¡£

ÆäËûÒþʽÀàÐÍת»»

Êý×éת»»³ÉÖ¸Õë

ÔÚ´ó¶àÊýÓõ½Êý×éµÄ±í´ïʽÖУ¬Êý×é×Ô¶¯×ª»»³ÉÖ¸ÏòÊý×éÊ×ÔªËصÄÖ¸Õ룻

µ±Êý×é±»ÓÃ×÷decltype¹Ø¼ü×ֵIJÎÊý£¬»òÕß×÷ΪȡµØÖ··û¡¢sizeof¡¢typeidµÈÔËËã·ûµÄÔËËã¶ÔÏóʱ£¬ÉÏÊöת»»²»»á·¢Éú¡£

Ö¸ÕëµÄת»»

ת»»³É²¼¶ûÀàÐÍ

Èç¹ûÖ¸Õë»òËãÊõÀàÐ͵ÄֵΪ0£¬×ª»»½á¹ûÊÇFALSE£¬·ñÔòÊÇTRUE

ת»»³É³£Á¿

ÔÊÐí½«·Ç³£Á¿ÀàÐ͵ÄÖ¸Õëת»»³ÉÖ¸ÏòÏàÓ¦µÄ³£Á¿ÀàÐ͵ÄÖ¸Õ룬¶ÔÓÚÒýÓÃÒ²ÊÇÕâÑù¡£Ïà·´µÄת»»²¢²»´æÔÚ£¬ÒòΪËüÊÔͼɾ³ýµôµ×²ãconst¡£

ÀàÀàÐͶ¨ÒåµÄת»»

ÏÔʽת»»

Ò²³ÆΪǿÖÆÀàÐÍת»»¡£

NOTE£ºÇ¿ÖÆÀàÐÍת»»ºÜΣÏÕ

ÃüÃûµÄÇ¿ÖÆÀàÐÍת»»

cast-name<type>(expression);

ÆäÖУ¬typeÊÇת»»µÄÄ¿±êÀàÐͶøexpressionÊÇҪת»»µÄÖµ¡£

cast-nameÊÇstatic_cast¡¢dynamic_cast¡¢const_castºÍreinterpret_castÖеÄÒ»ÖÖ¡£

static_cast

ÈκξßÓÐÃ÷È·¶¨ÒåµÄÀàÐÍת»»£¬Ö»Òª²»°üº¬µ×²ãconst£¬¶¼¿ÉÒÔʹÓÃstatic_cast.

µ±ÐèÒª°ÑÒ»¸ö½Ï´óµÄËãÊõÀàÐ͸³Öµ¸ø½ÏСµÄÀàÐÍʱ£¬static_cast·Ç³£ÓÐÓá£Ëü»áÈñàÒëÆ÷ÖªµÀÎÒÃDz»ÔÚºõ¾«¶ÈËðʧ£¬¶ø²»±¨¾¯¡£

static_cast¶ÔÓÚ±àÒëÆ÷ÎÞ·¨×Ô¶¯Ö´ÐеÄÀàÐÍת»»Ò²·Ç³£ÓÐÓá£

const_cast

const_castÖ»ÄܸıäÔËËã¶ÔÏóµÄµ×²ãconst¡£³£ÓÃÓÚÓк¯ÊýÖØÔصÄÉÏÏÂÎÄÖС£

Ö»ÓÐconst_castÄܸıä±í´ïʽµÄ³£Á¿ÊôÐÔ£¬Ê¹ÓÃÆäËûÐÎʽµÄÃüÃûÀàÐÍת»»¸Ä±ä±í´ïʽµÄ³£Á¿ÊôÐÔ¶¼½«Òý·¢±àÒëÆ÷´íÎó¡£

const char *p;
char *q = const_cast<char*>(p);

reinterpret_cast

ͨ³£ÎªÔËËã¶ÔÏóµÄλģʽÌṩ½ÏµÍ²ã´ÎÉϵÄÖØнâÊÍ¡£¾¡Á¿²»Ê¹Óá£Ö÷ÒªÊÇÖ¸ÕëÖ®¼äµÄת»»

int *ip;
char *q = reinterpret_cast<char*>(ip);

 

±êÇ©£º

°æȨÉêÃ÷£º±¾Õ¾ÎÄÕ²¿·Ö×ÔÍøÂ磬ÈçÓÐÇÖȨ£¬ÇëÁªÏµ£ºwest999com@outlook.com
Ìرð×¢Ò⣺±¾Õ¾ËùÓÐתÔØÎÄÕÂÑÔÂÛ²»´ú±í±¾Õ¾¹Ûµã£¬±¾Õ¾ËùÌṩµÄÉãÓ°ÕÕƬ£¬²å»­£¬Éè¼Æ×÷Æ·£¬ÈçÐèʹÓã¬ÇëÓëÔ­×÷ÕßÁªÏµ£¬°æȨ¹éÔ­×÷ÕßËùÓÐ

ÉÏһƪ£ºÀûÓÃOpenCV¸øͼÏñÌí¼ÓÖÐÎıê×¢

ÏÂһƪ£ºopenCVÅäÖÃ