¡¡¡¡C++º¯Êý´úÂëÒ²ºÍ¶ÔÏóÒ»Ñù£¬¶¼ÊDZ£´æÔÚÄÚ´æÖеģ¬ËùÒÔº¯ÊýÒ²ÊÇÓÐÄÚ´æµØÖ·µÄ¡£µ«ÊǺ¯ÊýÖ¸ÕëÊDz»ÓèÐíÐ޸ĵġ£Ö»¿ÉÒÔÓÐÁ½ÖÖ²Ù×÷ 1. µ÷Óú¯Êý£¬ 2 »ñÈ¡ÆäµØÖ·¡£
¡¡¡¡[cpp
¡¡¡¡void error£¨string s£© { /*º¯ÊýÄÚÈÝ*/ }
¡¡¡¡void £¨*efct£©£¨string£©£» // È·¶¨º¯ÊýÖ¸ÕëµÄÀà±ð
¡¡¡¡void f£¨£©
¡¡¡¡{
¡¡¡¡efct = &error; // efct points to error
¡¡¡¡efct£¨"error"£©£» // call error through efct
¡¡¡¡}
¡¡¡¡º¯ÊýÖ¸ÕëºÍÒ»°ãÖ¸Õ벻ͬ£¬ÓúͲ»ÓÃ*²Ù×÷¶¼ÊÇ¿ÉÒԵġ£
¡¡¡¡Í¬Àí£¬ÓúͲ»ÓÃ&²Ù×÷¶¼ÊÇ¿ÉÒԵģº
¡¡¡¡[cpp]
¡¡¡¡void £¨£¿f1£©£¨string£© = &error; // OK: same as = error
¡¡¡¡void £¨£¿f2£©£¨string£© = error; // OK: same as = &error
¡¡¡¡void g£¨£©
¡¡¡¡{
¡¡¡¡f1£¨"Vasa"£©£» //OK: same as £¨*f1£©£¨"Vasa"£©
¡¡¡¡£¨*f1£©£¨"Mary Rose"£©£» // OK: as f1£¨"Mary Rose"£©
¡¡¡¡}
¡¡¡¡º¯ÊýÖ¸ÕëÀàÐÍÒ»¶¨Òª×¢ÒâÆ¥Å䣺
¡¡¡¡[cpp]
¡¡¡¡void £¨*pf£©£¨string£©£» // pointer to void£¨string£©
¡¡¡¡void f1£¨string£©£» // void£¨str ing£©
¡¡¡¡int f2£¨string£©£» // int£¨string£©
¡¡¡¡void f3£¨int*£©£» //void£¨int*£©
¡¡¡¡[cpp]
¡¡¡¡void f£¨£©
¡¡¡¡{
¡¡¡¡pf = &f1; // OK
¡¡¡¡pf = &f2; // error : bad return type,should be pointer
¡¡¡¡pf = &f3; // error : bad argument type, should be pointer
¡¡¡¡pf£¨"Hera"£©£» // OK
¡¡¡¡pf£¨1£©£» //error : bad argument type
¡¡¡¡int i = pf£¨"Zeus"£©£» // error : void assigned to int
¡¡¡¡}
¡¡¡¡º¯ÊýÖ¸ÕëÊǹ㷺µØÓÃÔÚC-style´úÂëÖУ¬×÷ΪÆäËûº¯ÊýµÄ²ÎÊýµÄ¡£
¡¡¡¡²»¹ýÔÚC++ÖбȽÏÉÙÓÃÁË¡£C++Ó¦¸ÃʹÓÃfunctor,ÈçÏ£º
¡¡¡¡[cpp]
¡¡¡¡int main£¨£©
¡¡¡¡{
¡¡¡¡cout ¡¶ "Heads in alphabetical order:\n";
¡¡¡¡sort£¨heads.begin£¨£©£¬ head.end£¨£©£¬
¡¡¡¡[]£¨const User& x, const User& y£© { return x.name<y.name; }
¡¡¡¡£©£»
¡¡¡¡print_id£¨heads£©£»
¡¡¡¡cout ¡¶ '\n';
¡¡¡¡cout ¡¶ "Heads in order of department number:\n";
¡¡¡¡sort£¨heads.begin£¨£©£¬ head.end£¨£©£¬
¡¡¡¡[]£¨const User& x, const User& y£© { return x.dept<y.dept; }
¡¡¡¡£©£»
¡¡¡¡print_id£¨heads£©£»
¡¡¡¡}