1. ÒýÓõÄ×÷ÓãºÒýÓÃÊÇÓÃÓÚ¸ø±äÁ¿È¡±ðÃû
2. ÒýÓõĶ¨ÒåµÄ¸ñʽ£º
Êý¾ÝÀàÐÍ &±ðÃû = ±äÁ¿Ãû;
±ðÃûÊDZäÁ¿ÃûµÄÒýÓã¬
±ðÃûµÄÒýÓöÔÏóÊDZäÁ¿Ãû
ÒýÓõĶÔÏóÊDZäÁ¿£¬²»Êdz£Á¿£¬²»ÄܶԳ£Á¿½øÐÐÒýÓÃ
& £ºÒýÓÃÉùÃ÷·û
Êý¾ÝÀàÐÍ£º¸ú±äÁ¿ÃûµÄÊý¾ÝµÄÀàÐÍÊÇÒ»ÑùµÄ
3. ˵Ã÷£º
1£©Ê¹ÓÃÒýÓõÄʱºò±ØÐëÒª³õʼ»¯£¬¸úconst¹Ø¼ü×ÖÒ»Ñù£¬ÔÚ¶¨ÒåµÄͬʱ±ØÐë³õʼ»¯
//Ìص㣺
//1)ÒýÓñØÐëÒª³õʼ»¯£¬È·¶¨ÒýÓöÔÏó
//2)ÒýÓÃÒ»µ©³õʼ»¯£¬Ôò¹Ì¶¨ÒýÓ㬺óÃæÔÚʹÓõĹý³ÌÖУ¬ÒýÓõÄÄ¿±ê²»»á·¢Éú¸Ä±ä
//3)ÒýÓõı¾ÖÊ£ºÖ¸Õ룬ָÕëµÄÖ¸Ïò¹Ì¶¨Ö¸Ïò£¬----->Ö¸Õë³£Á¿
//4)ÒýÓñ¾ÉíÔÚϵͳռÓÃÒ»¿éÄÚ´æ¿Õ¼ä£¬ÄÚ´æ¿Õ¼äµÄ´óСһ°ã¹Ì¶¨Îª4¸ö×Ö½Ú£¨Ö¸ÕëËùÕ¼µÄ¿Õ¼ä´óС£©
// ÒýÓõĵØÖ·²»ÏÔʾ×Ô¼º£¬ÏÔʾµÄÊÇÒýÓöÔÏóµÄµØÖ·
#include <iostream> using namespace std;
int main() { int a = 1000; int b = 1111; int &pa = a; //pa ÊÇÕûÐͱäÁ¿aµÄÒýÓà //ÕûÐͱäÁ¿aÊÇpaµÄÒýÓöÔÏó(Ä¿±ê£© //ÒýÓý¨Á¢Íê³ÉÖ®ºó£¬paÊDZäÁ¿aµÄ±ðÃû,²»ÊÇ´´½¨±äÁ¿ //²Ù×÷pa¾ÍÏ൱ÓÚÔÚ²Ù×÷a£¬a == pa
cout << pa << endl; //pa¾ÍÊÇa
pa = b; //½«ÕûÐͱäÁ¿bµÄÖµ¸³Öµ¸øpa,Ò²¾ÍÊǸ³Öµ¸øa //paµÄÒýÓÃÄ¿±ê³õʼ»¯Ö®ºó£¬Ôò¹Ì¶¨ÒýÓà cout << pa << endl; cout << a << endl;
cout << &a << endl; cout << &pa << endl; //ÒýÓò»ÏÔʾ×Ô¼ºµÄµØÖ·£¬ÏÔʾµÄÒýÓöÔÏóµÄµØÖ· //ÒýÓñ¾ÉíÊÇÕ¼ÓÃÄÚ´æ¿Õ¼ä£¬²¢ÇÒpaµÄÄÚ´æ¿Õ¼äʵ¼ÊÉÏ´æ´¢µÄÊÇaµÄµØÖ·
} |
4. ÒýÓÃËùÕ¼µÄ¿Õ¼ä´óС£¬ÒýÓõı¾ÖÊÊÇÖ¸Õ룬ËùÒÔ¿Õ¼ä´óС¸úÖ¸ÕëËùÕ¼µÄ¿Õ¼ä´óСÊÇÒ»ÑùµÄ£¬Ò»°ãΪËĸö×Ö½Ú
class AB { char &a; char &b; }; //ABÀàÐÍ------->Êý¾ÝÀàÐÍ
//ÀàËùÕ¼µÄ¿Õ¼ä´óСµÄ¼ÆËã¸ú½á¹¹ÌåËùÕ¼µÄ¿Õ¼ä´óСµÄ¼ÆËãÊÇÒ»Ñù int main() { cout << sizeof(AB) << endl; } |
5. ÒýÓõı¾ÖÊÊÇÖ¸Õ룬Êǹ̶¨Ö¸ÏòµÄÒýÓã¬ÒýÓõĿռäʵ¼ÊÊÇ´æ´¢ÒýÓöÔÏóµÄµØÖ·£¬Ö»ÊÇÔÚ´òÓ¡ÒýÓõĵØÖ·µÄʱºòÏÔʾÊÇÒýÓöÔÏóµÄµØÖ·
#include <iostream> #include <stdio.h>
//ÑéÖ¤ÐèÒªÔÚVC»·¾³Ï using namespace std; int main() { int a = 100; int &pa = a; //paÊÇÕûÐͱäÁ¿aµÄÒýÓà int c = 1000;
cout << &a << endl; //0X0019FF3C cout << &pa << endl; //²»ÏÔʾ×Ô¼ºµÄµØÖ·£¬ÏÔʾµÄÊÇÒýÓöÔÏóµÄµØÖ· 0X0019FF3C cout << &c << endl; //0X0019FF34
//¿ÉÒÔÍÆËã³öÀ´£¬pa±¾ÉíµÄµØÖ· 0X0019FF38£¬¶Ô¸ÃµØÖ·È¡ÄÚÈÝ£¬Èç¹û¶Á³öÀ´µÄÊÇaµÄµØÖ· //Ôò¿ÉÒÔ˵Ã÷ÒýÓÃ(pa)±¾ÖÊÊÇÒ»¸ö¹Ì¶¨Ö¸ÏòµÄÖ¸Õ룬ָÕë³£Á¿£¬ printf("%#p\n",*((int *)0X0019FF38)); //ÖµÊÇ 0X0019FF3C } |
6. ÒýÓÃ×÷Ϊº¯ÊýµÄ²ÎÊý´«µÝ
º¯ÊýµÄ²ÎÊý´«µÝµÄ·½Ê½£º
1£©Öµ´«µÝ£º½«Êµ²ÎµÄÖµ´«µÝ¸øÐβαäÁ¿£¬Ðβθı䲻ӰÏìʵ²ÎµÄ±ä»¯
2£©Ö·´«µÝ£º½«Êµ²ÎµÄµØÖ·´«µÝ¸øÐβαäÁ¿(Ö¸Õë)£¬Ðβθıäʵ²ÎÒ²»á¸ú×ű仯
3£©ÒýÓô«µÝ£ºÐ§¹û¸úÖ·´«µÝÊÇÒ»Ñù£¬ÔÚC++ÖУ¬ÄÜÓÃÖ¸Õë×÷Ϊº¯ÊýµÄ²ÎÊý´«µÝ£¬»ù±¾¶¼¿ÉÒÔ¸ÄΪÒýÓÃ×÷Ϊº¯ÊýµÄ²ÎÊý
7. ÒýÓÃ×÷Ϊº¯ÊýµÄ·µ»ØÖµ
ÒýÓÃÊÇÒýÓÃÄ¿±ê±äÁ¿µÄ±ðÃû£¬²Ù×÷ÒýÓã¬Ôò¾ÍÊÇÔÚ²Ù×÷Ä¿±ê±äÁ¿£¬µ±Ä¿±ê±äÁ¿¿Õ¼äÔÚÕ»Çø(¾Ö²¿±äÁ¿)£¬±äÁ¿ËùÔڵĺ¯Êý½áÊø£¬(Õ»Çø)¿Õ¼ä±»ÊÍ·Å£¬Ä¿±ê±äÁ¿µÄ¿Õ¼ä³·Ïú£¬´Ëʱ¸ÃÄ¿±ê±äÁ¿µÄÒýÓÃÒ²»áËæ×ÅÄ¿±ê±äÁ¿Ò»ÆðÏûʧ£¬ ÊÍ·ÅÖ¸ÕëÖ¸ÏòµÄ¿Õ¼äµÄʱºò£¬Ö¸Õë×îºóÒª±»Öÿգ¬ÒýÓò»ÐèÒªÕâÑùµÄ²Ù×÷
ÒýÓÃÊÇÓëÄ¿±ê±äÁ¿¹²´æÍö£¬ÒýÓÃÔÚ×÷Ϊº¯ÊýµÄ·µ»ØÖµµÄʱºò£¬²»ÄÜ·µ»Ø¾Ö²¿±äÁ¿µÄÒýÓÃ(³ýÁ˾²Ì¬¾Ö²¿±äÁ¿)£¬¾Ö²¿±äÁ¿»áËæןñäÁ¿ËùÔڵĺ¯ÊýµÄ½áÊø¶øÊÍ·Å£¬´Ëʱ·µ»Ø¾Ö²¿±äÁ¿µÄÒýÓÃÒ²ÊÇÎÞÓõÄ
#include <iostream>
using namespace std;
void swap(int &pa, int &pb); int & testFun(int num); int & testFunction(int &num); int main() { int a = 10; int b = 20;
swap(a, b);
cout << "a = " << a << " ,b = " << b << endl;
a = 100;
//int &pNum = testFun(a); //·µ»Ø¾Ö²¿±äÁ¿µÄÒýÓ㨷Ǿ²Ì¬¾Ö²¿±äÁ¿£©
//cout << pNum << endl; //ÒýÓûáËæ×ÅÄ¿±ê±äÁ¿Ò»ÆðÏûʧ
int & pfun = testFunction(a); //pfun ÊÇaµÄÒýÓà pfun = 10000; cout << a << endl;
}
void swap(int &pa, int &pb) //int &pa = a,int &pb = b { int tmp;
tmp = pa; pa = pb; pb = tmp; }
//º¯ÊýµÄ·µ»ØÖµÀàÐÍΪÒýÓÃÀàÐÍ int & testFun(int num) { //int num ÊǾֲ¿±äÁ¿£¬º¯Êý½áÊø£¬¿Õ¼äÊÍ·Å
num = num + 100;
return num; //·µ»ØµÄÊÇnum±äÁ¿µÄÒýÓà //º¯Êý½áÊønum¿Õ¼äÊÍ·Å }
int & testFunction(int &num) //int &num = a; { //int &num = a; //num ÊÇʵ²Î±äÁ¿aµÄÒýÓÃ
num = num + 100;
return num; //·µ»ØµÄÊÇnumµÄÒýÓà //numµÄ¿Õ¼äûÓÐÊÍ·Å£¬ÒòΪÒýÓöÔÏóµÄ¿Õ¼äûÓÐÊÍ·Å } |
8. ÒýÓþßÓеÄƽÐд«µÝ¹Øϵ
int a =10;
int &pa = a; //paÊÇaµÄÒýÓà pa == a
int &pb = pa; //pbÊÇpaµÄÒýÓà pb == pa
//ËùÒÔ£ºpbÊÇaµÄÒýÓ㬠pb == pa == a
#include <iostream>
using namespace std;
int main() { int a = 10; int &pa = a; //pa ÊÇa µÄÒýÓã¬--->pa == a; int &pb = pa; //pb ÊÇpaµÄÒýÓ㬠--->pb == pa
//Êý¾ÝÀàÐÍ *------>Ö¸ÕëÀàÐÍ //Êý¾ÝÀàÐÍ & ----->ÒýÓÃÀàÐÍ //½áÂÛ£º pb ÊÇaµÄÒýÓà pb == pa == a;
pb = 1000;
cout << pa << endl; cout << a << endl;
pa = 1111; cout << pb << endl; cout << a << endl;
}
|
9. ×ܽ᣺
(1) ÒýÓÃÊǶÔÒýÓÃÄ¿±êÆð±ðÃû£¬ÒýÓõĶÔÏó±ØÐëÊDZäÁ¿
(2) ÒýÓñ¾ÖÊÊÇÖ¸Õ룬һµ©³õʼ»¯£¬ÔòÒýÓþ͹̶¨Ö¸Ïò£¬Êµ¼ÊÉÏÊdz£Á¿Ö¸Õ룬ÔÚÄÚ´æÖÐËùÕ¼µÄ¿Õ¼ä´óСһ°ãΪ4¸ö×Ö½Ú
(3) ÒýÓþßÓÐƽÐд«µÝµÄ¹Øϵ
(4) ÒýÓÃÒ»°ã×÷Ϊº¯ÊýµÄÐβΣ¬±ÈÖ¸Õë¸ü·½±ã£¬Í¨Ë×
(5) ÒýÓÃÈç¹û×÷Ϊº¯ÊýµÄ·µ»ØÖµ£¬Ôò²»ÄÜ·µ»Ø¾Ö²¿±äÁ¿µÄÒýÓÃ(·Ç¾²Ì¬¾Ö²¿±äÁ¿)
(6) ÒýÓûáËæ×ÅÄ¿±ê±äÁ¿Ò»ÆðÏûʧ£¬Ä¿±ê±äÁ¿¿Õ¼äÊÍ·Å£¬ÔòÒýÓÃÒ²¸ú×ÅÏûʧ
(7) ÒýÓò»ÊÇ´´½¨±äÁ¿£¬½ö½öÖ»ÊǶÔÄ¿±ê±äÁ¿Æð±ðÃû£¬²Ù×÷±ðÃû¾ÍÊÇÔÚ²Ù×÷Ä¿±ê±äÁ¿