c引用和指針的區(qū)別
推薦 + 挑錯(cuò) + 收藏(0) + 用戶評(píng)論(0)
指針和引用的定義
在深入介紹之前我們首先來(lái)看一下指針和引用的定義、指針和引用的區(qū)別,然后分別針對(duì)指針和引用展開(kāi)討論,深入細(xì)節(jié)為何有這些差異。
指針的權(quán)威定義:
In a declaration T D where D has the form * cv-qualifier-seqopt D1 And the type of the identifier in the declaration T D1 is “derived-declarator-type-list T”, then the type of the identifier of D is “derived-declarator-type-list cv-qualifier-seq pointer to T”。 The cv-qualifiers apply to the pointer and not to the object pointer to.
——摘自《ANSI C++ Standard》
注:可能有些讀者并不明白cv-qualifier-seq
CV-qualifiers(CV限定符)
CV-qualifiers有三種:const-qualifier(const限定符)、Volatile-qualifier(volatile限定符)、以及const-volatile-qualifier(const-volatile限定符)。
const類(lèi)對(duì)象的非靜態(tài)、非mutable、以及非引用數(shù)據(jù)成員是const-qualified;
volatile類(lèi)對(duì)象的非靜態(tài)、非引用數(shù)據(jù)成員是volatile-qualified;
const-volatile類(lèi)對(duì)象的非靜態(tài)、非引用數(shù)據(jù)成員是const-volatile-qualified。
當(dāng)CV-qualifiers用于限定數(shù)組類(lèi)型時(shí),實(shí)際上是數(shù)組成員被該CV-qualifiers限定,而非該數(shù)組類(lèi)型。
復(fù)合類(lèi)型并不因其成員被CV-qualifier限定而被該CV-qualifier限定,也就是說(shuō),即使復(fù)合類(lèi)型的成員有CV-qualifier限定,該復(fù)合類(lèi)型也不是CV-qualified對(duì)象。
引用的權(quán)威定義:
In a declaration T D where D has the form& D1 And the type of the identifier in the declaration T D1 is “derived-declarator-type-list T”, then the type of the identifier of D is “derived-declarator-type-list cv-qualifier-seq reference to T”。 Cv-qualified references are ill-formed except when the cv-qualifiers are introduced through the use of a typedef or a template type argument, in which case the cv-qualifiers are ignored.
——摘自《ANSI C++ Standard》
上面這些定義初看有些難懂,如果是這樣的話,那說(shuō)明你對(duì)C++還不夠熟悉,你還有很長(zhǎng)的路要走。下面用通俗易懂的話來(lái)概述一下:
指針-對(duì)于一個(gè)類(lèi)型T,T*就是指向T的指針類(lèi)型,也即一個(gè)T*類(lèi)型的變量能夠保存一個(gè)T對(duì)象的地址,而類(lèi)型T是可以加一些限定詞的,如const、volatile等等。見(jiàn)下圖,所示指針的含義:
引用-引用是一個(gè)對(duì)象的別名,主要用于函數(shù)參數(shù)和返回值類(lèi)型,符號(hào)X&表示X類(lèi)型的引用。見(jiàn)下圖,所示引用的含義:
非常好我支持^.^
(0) 0%
不好我反對(duì)
(0) 0%
下載地址
c引用和指針的區(qū)別下載
相關(guān)電子資料下載
- 常見(jiàn)的一級(jí)指針參數(shù)有什么作用 72
- 函數(shù)類(lèi)型和函數(shù)指針類(lèi)型的區(qū)別 18
- 一文詳解ZGC關(guān)鍵技術(shù) 26
- C語(yǔ)言編寫(xiě)程序執(zhí)行效率高的原因是什么? 31
- C編程中指針的語(yǔ)法和工作原理 152
- PLC指針類(lèi)型與間接尋址如何使用 26
- JVM指針壓縮的工作原理 66
- 指針式萬(wàn)用表和數(shù)字式萬(wàn)用表使用區(qū)別 117
- Linux整體匯編啟動(dòng)流程分析 122
- 如何使用指針數(shù)據(jù)包定義數(shù)據(jù)緩沖區(qū) 71