引用變數是已定義變數的別名,是c++中新增的資料型別。它可以看做是指標的另一種表示方法(但不完全相同),常被用來作為函式的形參。
int a;
int& b = a;
b即為乙個指向a的引用變數
1)希望能夠在子函式中修改呼叫函式中的資料
2)通過傳遞引用變數,可以提高程式的執行速度(因為按值傳遞的話需要建立臨時的變數,會使程式執行速度變慢)。
引用變數在宣告時必須進行初始化,不允許為空;同時一旦與某個變數關聯起來,就一直與它關聯,不允許改變。
而指標變數可以不進行初始化,可以為空,能夠改變其指向。
//strc_ref.cpp -- using structure references
#include
#include
struct free_throws
;void
display
(const free_throws & ft)
;void
set_pc
(free_throws & ft)
;free_throws &
accumulate
(free_throws &target,
const free_throws &source)
;int
main()
; free_throws two =
; free_throws three =
; free_throws four =
; free_throws five =
; free_throws team =
; free_throws dup;
set_pc
(one)
;display
(one)
;accumulate
(team, one)
;display
(team)
;// use return value as argument
display
(accumulate
(team, two));
accumulate
(accumulate
(team, three)
, four)
;display
(team)
;// use return value in assignment
dup =
accumulate
(team,five)
; std:
:cout <<
"displaying team:\n"
;display
(team)
; std:
:cout <<
"displaying dup after assignment:\n"
;display
(dup)
;set_pc
(four)
;// ill-advised assignment
accumulate
(dup,five)
= four;
std:
:cout <<
"displaying dup after ill-advised assignment:\n"
;display
(dup)
;// std::cin.get();
return0;
}void
display
(const free_throws & ft)
void
set_pc
(free_throws & ft)
free_throws &
accumulate
(free_throws & target,
const free_throws & source)
儲存描述
持續性作用域
鏈結性如何宣告
自動自動
**塊無
在**塊中直接宣告變數
暫存器自動
**塊無
在**塊中使用關鍵字register
靜態,無鏈結性
靜態**塊
無在**塊中使用關鍵字static
靜態,外部鏈結性
靜態檔案
外部不在任何函式內
靜態,內部鏈結性
靜態檔案
內部不在任何函式內,使用關鍵字static
#include
intmain()
#include
intmain()
#include
intmain()
using宣告就像是宣告了乙個變數,如果當前函式中已經有這個名稱了,就不能使用using宣告,但可以使用using編譯指令,這個時候命名空間中的名稱將被區域性的名稱覆蓋掉。
#include
intmain()
#include
intmain()
第三週學習筆記
我們可以通過熟練掌握vim來提高我們編輯檔案的效率 1.dd 剪下游標所在行 2.ndd 剪下游標所在之後n行 3.yy 複製游標所在行 4.nyy 複製游標所在之後n行 5.p 將貼上板中資料貼上在游標下一行 6.p 將貼上板中資料貼上在游標上一行 7.u 復原上乙個操作 8.ctrl r 取消上...
第三週學習筆記
直流電動機 分類 按照勵磁方式可以分為自勵和他勵。我們主要學習的是他勵直流電動機,這種直流電動機採用的是獨立的勵磁電源進行勵磁,因此工作起來相當於乙個永磁體 不改變勵磁電路特性的情況下 分析方法 主要採用功率法和電路中的歐姆定律進行分析,基本點是在實際的機械系統中速度不能發生突變,而電機的特性只能沿...
Python第三週學習筆記
課時10 指數 兩個乘號就是指數 例 a 7 2 print a 49 比較運算子 對兩個內容進行比較的運算子 i 結果一定是布林值即false true.賦值運算子 把乙個值放到變數裡邊去 賦值符號 例a 9 print a 9 複雜賦值 a b 9 a,b 1,2 賦值的縮寫 a 0 a a 3...