C++µÄËÄÖÖcast²Ù×÷·ûµÄÇø±ð
dynamic_cast: ͨ³£ÔÚ»ùÀàºÍÅÉÉúÀàÖ®¼äת»»Ê±Ê¹ÓÃ,run-time cast
const_cast: Ö÷ÒªÕë¶ÔconstºÍvolatileµÄת»».
static_cast: Ò»°ãµÄת»»£¬no run-time check.ͨ³££¬Èç¹ûÄã²»ÖªµÀ¸ÃÓÃÄĸö£¬¾ÍÓÃÕâ¸ö¡£
reinterpret_cast: ÓÃÓÚ½øÐÐûÓÐÈκιØÁªÖ®¼äµÄת»»£¬±ÈÈçÒ»¸ö×Ö·ûÖ¸Õëת»»ÎªÒ»¸öÕûÐÎÊý¡£
C++µÄËÄÖÖcast²Ù×÷·ûµÄÇø±ð
·¢ÐÅÕ¾: ˮľÉçÇø (Thu Jan 26 21:15:16 2006), Õ¾ÄÚ
ÉùÃ÷ by NetMD£º
²¢·ÇÎÒµÄÔ´´£¬À´×Ô»¥ÁªÍø£¬ÇÒÊÇÁ½ÆªÌû×ӵĺϼ¯£¬¸öÈ˾õµÃÕâÑù²Å±È½ÏÍ걸
----------------------------------------------------------------------
Q:ʲôÊÇC·ç¸ñת»»£¿Ê²Ã´ÊÇstatic_cast, dynamic_cast ÒÔ¼° reinterpret_cast£¿Çø±ðÊÇʲô£¿ÎªÊ²Ã´Òª×¢Ò⣿
A:ת»»µÄº¬ÒåÊÇͨ¹ý¸Ä±äÒ»¸ö±äÁ¿µÄÀàÐÍΪ±ðµÄÀàÐÍ´Ó¶ø¸Ä±ä¸Ã±äÁ¿µÄ±íʾ·½Ê½¡£ÎªÁËÀàÐÍת»»Ò»¸ö¼òµ¥¶ÔÏóΪÁíÒ»¸ö¶ÔÏóÄã»áʹÓô«Í³µÄÀàÐÍת»»²Ù×÷·û¡£±ÈÈ磬ΪÁËת»»Ò»¸öÀàÐÍΪdouboleµÄ¸¡µãÊýµÄÖ¸Õëµ½ÕûÐÍ£º
´úÂë:
int i;
double d;
i = (int) d;
»òÕߣº
i = int (d);
¶ÔÓÚ¾ßÓбê×¼¶¨Òåת»»µÄ¼òµ¥ÀàÐͶøÑÔ¹¤×÷µÄºÜºÃ¡£È»¶ø£¬ÕâÑùµÄת»»·ûÒ²Äܲ»·ÖÔí°×µÄÓ¦ÓÃÓÚÀࣨclass£©ºÍÀàµÄÖ¸Õë¡£ANSI-C++±ê×¼¶¨ÒåÁËËĸöеÄת»»·û£º'reinterpret_cast', 'static_cast', 'dynamic_cast' ºÍ 'const_cast'£¬Ä¿µÄÔÚÓÚ¿ØÖÆÀà(class)Ö®¼äµÄÀàÐÍת»»¡£
´úÂë:
reinterpret_cast<new_type>(expression)
dynamic_cast<new_type>(expression)
static_cast<new_type>(expression)
const_cast<new_type>(expression)
1 reinterpret_cast
'reinterpret_cast'ת»»Ò»¸öÖ¸ÕëΪÆäËüÀàÐ͵ÄÖ¸Õë¡£ËüÒ²ÔÊÐí´ÓÒ»¸öÖ¸Õëת»»ÎªÕûÊýÀàÐÍ¡£·´Ö®ÒàÈ»¡££¨Òë×¢£ºÊÇÖ¸Õë¾ßÌåµÄµØÖ·Öµ×÷ΪÕûÊýÖµ£¿£©
Õâ¸ö²Ù×÷·ûÄܹ»ÔÚ·ÇÏà¹ØµÄÀàÐÍÖ®¼äת»»¡£²Ù×÷½á¹ûÖ»ÊǼòµ¥µÄ´ÓÒ»¸öÖ¸Õëµ½±ðµÄÖ¸ÕëµÄÖµµÄ¶þ½øÖÆ¿½±´¡£ÔÚÀàÐÍÖ®¼äÖ¸ÏòµÄÄÚÈݲ»×öÈκÎÀàÐ͵ļì²éºÍת»»¡£
Èç¹ûÇé¿öÊÇ´ÓÒ»¸öÖ¸Õëµ½ÕûÐ͵Ŀ½±´£¬ÄÚÈݵĽâÊÍÊÇϵͳÏà¹ØµÄ£¬ËùÒÔÈκεÄʵÏÖ¶¼²»ÊÇ·½±ãµÄ¡£Ò»¸öת»»µ½×ã¹»´óµÄÕûÐÍÄܹ»°üº¬ËüµÄÖ¸ÕëÊÇÄܹ»×ª»»»ØÓÐЧµÄÖ¸ÕëµÄ¡£
´úÂë:
class A {};
class B {};
A * a = new A;
B * b = reinterpret_cast<B *>(a);
'reinterpret_ca
Ïà¹ØÎĵµ£º
×î½üһֱæ±ÏÒµµÄÏà¹ØÊÂÇ飬¼ÓÉϹ¤×÷£¬×ªÑۼ䣬ÓÖµ½Ôµ×ÁË£¬Ö®Ç°³ÐŵµÄÿÔÂһƪ²©ÎÄ£¬Ç°¼¸Ìì¾ÍÒ»Ö±ÔÚÑ°ÕÒµ½µ×Ҫдʲô£¬½üÁ½ÌìÓÖͻȻ·¢ÏÖÓкܶණÎ÷¿ÉÒÔд¡£±¾Æª¾ÍÏÈÑÓÐø֮ǰµÄһƪ»ùÓÚCookieµÄ°²È«¼ì²é»úÖÆ£¨ÉîÈëC/C++Ö®»ùÓÚCookieµÄ°²È«¼ì²é£¨VS2005£©£©À´½éÉÜÏÂÁíÍâÒ»ÖÖÔÚDEBUG°æ±¾Ïµİ²È«¼ì²é£¬Ò²¾ÍÊÇCheckStackVars¼ì ......
LinuxÏÂC++¿ª·¢¹¤¾ß½éÉÜ
¡¡¡¡¸ÅÊö
¡¡¡¡¾ÍC£«£«¿ª·¢¹¤¾ß¶øÑÔ£¬ÓëWindowsÏÂ΢Èí£¨VC£¬ VS2005µÈ£©Ò»Í³ÌìÏÂÏà±È£¬Linux/UnixÏÂC£«£«¿ª·¢£¬¿ÉνÎ廨°ËÃÅ£¬¸÷ʽ¸÷Ñù¡£Emacs, vi, eclipse, anjuta£¬kdevelopµÈ²ã³ö²»Çî¡£
¡¡¡¡WindowsÏ£¬¿ª·¢¹¤¾ß¶àÒÔ¼¯³É¿ª·¢»·¾³IDEµÄÐÎʽչÏÖ¸ø×îÖÕÓû§¡£ÀýÈ磬VS2005¼¯³ ......
ÎÒÓõÄLinuxϵͳÊÇUbuntu 10.04
Ê×ÏÈ, °²×°wxWidgets 2.8. Ä¿Ç°wxWidgetsÓÐÁ½¸öÎȶ¨°æ±¾, 2.6ºÍ2.8, ÎÒÃÇÑ¡×îеÄ. ´ò¿ª “Ubuntu
Èí¼þÖÐÐÄ”, ÔÚËÑË÷¿òÀïÊäÈë “wxWidgets”, °ÑÏÂÃ漸¸öÃû×ÖÒÔ”wxWidgets Cross-platform C++ GUI
toolkit”¿ªÍ·µÄÈ«²¿°²×°. ×°Íêºó, ´ò¿ªÒ»¸öÖÕ¶Ë, ÊäÈ ......
C/C++
Í·ÎļþÒ»ÀÀ
//////////////////////////////////////////////////////////////////////////
C
Í·Îļþ
(C89,C95)
(C++98,C++03Ò²°üº¬)
include <assert.h>¡¡¡¡¡¡¡¡//Õï¶Ï¿â
include <ctype.h>¡¡¡¡¡¡¡¡ //×Ö·û´¦Àíº¯Êý¿â
include <errno.h>¡¡¡¡¡¡¡¡ //´íÎó¶¨Òå
include <float.h& ......