C++ 引用

 

2017-07-01

Microsoft Visual Studio ULtimate 2013 版本:12.0.21055.1 REL

 

1、引用

引用變量是一個別名,也就是說,它是某個已存在變量的另外一個名字。一旦把引用初始化爲某個變量,就能夠使用該引用名稱或變量名稱來指向變量。ios

 1 #include <stdlib.h>
 2 #include <iostream>
 3 
 4 int main()  5 {  6     int value = 123;  7     int &_value = value;//定義一個引用
 8     std::cout << "value 的地址:" << &value << std::endl;  9     std::cout << "_value 的地址:" << &_value << std::endl; 10 
11     std::cout << "value :" << value << std::endl; 12     std::cout << "value :" << _value << std::endl; 13     std::cout << "\n"; 14     system("pause"); 15 }
引用

運行結果:ide

從上述結果能夠看出,變量value與其引用變量_value指向了同一塊內存,使用引用並未開闢一塊新的內存。函數


2、引用與指針的區別

  1. 不存在空引用。引用必須鏈接到一塊合法的內存(可能因使用的編譯器不一樣,存在不一樣的狀況)。
  2. 一旦引用被初始化爲一個對象,就不能被指向到另外一個對象。指針能夠在任什麼時候候指向到另外一個對象。
  3. 引用必須在建立時被初始化。指針能夠在任什麼時候間被初始化。

1. 不存在空引用。spa

 1 #include <stdlib.h>
 2 #include <iostream>
 3 
 4 int main()  5 {  6     int value;  7     int &_value = value;  8     std::cout << "value 的地址:" << &value << std::endl;  9     std::cout << "_value 的地址:" << &_value << std::endl; 10 
11     std::cout << "value :" << value << std::endl; 12     std::cout << "value :" << _value << std::endl; 13 
14     std::cout << "\n"; 15     system("pause"); 16 }
引用不能爲空

第一次運行結果:3d

 

第二次運行結果:指針

 

由於value爲空,因此隨意指向了一個了一塊內存,雖然引用仍起做用,但不存在任何實際意義。(可能因使用的編譯器不一樣,存在不一樣的狀況,但建議不要使用空引用)code

2.  一旦引用被初始化爲一個對象,就不能被指向到另外一個對象對象

 1 #include <stdlib.h>
 2 #include <iostream>
 3 #include <string>
 4 
 5 int main()  6 {  7     std::string str = "Hello !!!";  8 
 9     std::string &_str = str; 10     std::cout << "str 的地址:" << &str << std::endl; 11     std::cout << "_str 的地址:" << &_str << std::endl; 12     std::cout << "str :" << str << std::endl; 13     std::cout << "_str :" << _str << std::endl; 14 
15     std::cout << std::endl; 16     std::string str_s = "Hello C++ !!!"; 17     _str = str_s; 18     std::cout << "str_s 的地址:" << &str_s << std::endl; 19     std::cout << "_str 的地址:" << &_str << std::endl; 20     std::cout << "str_s :" << str_s << std::endl; 21     std::cout << "_str :" << _str << std::endl; 22 
23     std::cout << std::endl; 24     std::cout << "str :" << str << std::endl; 25 
26     std::cout << "\n"; 27     system("pause"); 28 }
一旦引用被初始化爲一個對象,就不能被指向到另外一個對象

運行結果:blog

 

由運行結果可知,當嘗試對一個引用變量_str第二次賦值時,實際該引用變量_str並未指向新的變量str_s,而是改變了原變量str的值,所以 一旦引用被初始化爲一個對象,就不能被指向到另外一個對象。內存

3.  引用必須在建立時被初始化。

 1 #include <stdlib.h>
 2 #include <iostream>
 3 #include <string>
 4 void myString();  5 
 6 int main()  7 {  8     std::string str = "Hello !!!";  9     std::string &_str; 10     _str = str; 11 
12     std::cout << "str 的地址:" << &str << std::endl; 13     std::cout << "_str 的地址:" << &_str << std::endl; 14     std::cout << "str :" << str << std::endl; 15     std::cout << "_str :" << _str << std::endl; 16 
17     std::cout << "\n"; 18     system("pause"); 19 }
引用必須在建立時被初始化。

運行結果:

引用必須在建立時被初始化。


3、引用的用途

  1. 把引用做爲參數。

  2. 把引用做爲返回值

1.   把引用做爲參數

 1 #include <stdlib.h>
 2 #include <iostream>
 3 #include <string>
 4 
 5 void founcation(std::string str);  6 
 7 int main()  8 {  9     std::string str = "Hello !!!"; 10     std::cout << "主函數 :" << &str << std::endl; 11  founcation(str); 12 
13     std::cout << "\n"; 14     system("pause"); 15 } 16 
17 void founcation(std::string str) 18 { 19     std::cout << "函數執行 :" << &str << std::endl; 20     return; 21 }
不使用引用做爲參數

運行結果:

 1 #include <stdlib.h>
 2 #include <iostream>
 3 #include <string>
 4 
 5 void founcation(const std::string &str);  6 
 7 int main()  8 {  9     std::string str = "Hello !!!"; 10     std::cout << "主函數 :" << &str << std::endl; 11  founcation(str); 12 
13     std::cout << "\n"; 14     system("pause"); 15 } 16 
17 void founcation(const std::string &str) 18 { 19     std::cout << "函數執行 :" << &str << std::endl; 20     return; 21 }
把引用做爲參數

運行結果:

把引用做爲參數時,不會在函數調用是建立新的變量,不會開闢新的內存空間。
而不使用引用做爲參數則會在函數調用是建立新的變量,開闢一塊新的內存空間。

 2.    把引用做爲返回值

 1 #include <stdlib.h>
 2 #include <iostream>
 3 #include <string>
 4 
 5 std::string founcation(std::string &str);  6 
 7 int main()  8 {  9     std::string str = "Hello !!!"; 10     std::cout << "主函數 :" << &str << std::endl; 11 
12     std::cout << "返回值 :" << &founcation(str) << std::endl; 13 
14     std::cout << "\n"; 15     system("pause"); 16 } 17 
18 std::string founcation(std::string &str) 19 { 20     std::cout << "函數執行 :" << &str << std::endl; 21     return str; 22 }
不使用引用做爲函數返回值

運行結果:

 1 #include <stdlib.h>
 2 #include <iostream>
 3 #include <string>
 4 
 5 std::string& founcation(std::string &str);  6 
 7 int main()  8 {  9     std::string str = "Hello !!!"; 10     std::cout << "主函數 :" << &str << std::endl; 11 
12     std::cout << "返回值 :" << &founcation(str) << std::endl; 13 
14     std::cout << "\n"; 15     system("pause"); 16 } 17 
18 std::string& founcation(std::string &str) 19 { 20     std::cout << "函數執行 :" << &str << std::endl; 21     return str; 22 }
使用引用做爲函數返回值

運行結果:

使用引用做爲函數返回值,並未開闢新的內存空間,不適用函數做爲返回值,則會開闢新的內存空間。

相關文章
相關標籤/搜索