1. 程式人生 > >Guru of the Week 條款01: 變數的初始化

Guru of the Week 條款01: 變數的初始化

GotW #01 Variable Initialization

著者:Herb Sutter

翻譯:kingofark

[宣告]:本文內容取自www.gotw.ca網站上的Guru of the Week欄目,其著作權歸原著者本人所有。譯者kingofark在未經原著者本人同意的情況下翻譯本文。本翻譯內容僅供自學和參考用,請所有閱讀過本文的人不要擅自轉載、傳播本翻譯內容;下載本翻譯內容的人請在閱讀瀏覽後,立即刪除其備份。譯者kingofark對違反上述兩條原則的人不負任何責任。特此宣告。

Revision 1.0

Guru of the Week 條款01: 變數的初始化<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

難度:4 / 10

(想想看,有多少種將變數初始化的方法? 千萬要注意那些看上去很像“變數初始化”的東西。)

[問題]

下列四條語句有什麼區別嗎?

SomeType t = u;

SomeType t(u);

SomeType t();

SomeType t;

[解答]

我們按從下往上的順序逐個考察四條語句:

*SomeType t;

變數t被預設建構函式SomeType::SomeType()初始化。

*SomeType t();

這是一個騙局,因為這條語句看上去很像一個變數宣告,而實際上卻是一個函式宣告;這個函式t沒有引數並且返回型別為SomeType

*SomeType t(u);

這是直接初始化。變數

t通過建構函式SomeType::SomeType(u)被初始化。

*SomeType t = u;

這是拷貝初始化。變數t通過SomeType的拷貝建構函式(Copy Constructor)被初始化。(注意,這條語句雖然含有“=”,但仍然是一個初始化操作,而不是一個賦值操作,因為在這裡,允許使用'='只是為了可以沿用C語言的語法,operator=是不會被呼叫的。)

[語義學參考]:如果u恰好也是SomeType型別,那麼這條語句與“SomeType t(u);”是等同的,將呼叫SomeType的拷貝建構函式(Copy Constructor)。如果uSomeType以外的其它型別,那麼這條語句與“

SomeType t(SomeType(u))”是等同的。可以看到,在語句“SomeType t(SomeType(u))”裡,u被轉換成一個臨時的SomeType物件,而t則是由此拷貝構造出來的。

[注意]:在這種情況下,編譯器通常可以(但不是必須要)對其進行優化,適當的處理拷貝構造(Copy Construction)操作(一般是省略掉拷貝構造過程)。如果進行了優化,則一定要保證拷貝建構函式(Copy Constructor)的可達性。

[學習指導]建議總是使用“SomeType t(u)”的形式, 一來是因為只要可以用“SomeType t = u”的地方也同樣可以它;二來是因為它還有一些其它的優點,比如支援多個引數等。

相關推薦

Guru of the Week 條款01: 變數初始

GotW #01 Variable Initialization 著者:Herb Sutter 翻譯:kingofark [宣告]:本文內容取自www.gotw.ca網站上的Guru of the Week欄目,其著作權歸原著者本人所有。譯者kingofark在未經原著者本人

Guru of the Week 條款22:物件的生存期(第一部分)

GotW #22 Object Lifetimes – Part I<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 著者:Herb Sutter 翻譯:K ]

Guru of the Week 條款30附錄:介面原則

它是這麼工作的:所謂“名稱搜尋”就是當你寫下一個“f(parm)”呼叫時,編譯器必須要決策出你想調哪個叫f的函式。(由於過載和作用域的原因,可能會有幾個叫f的函式。)Koenig lookup是這麼說的,如果你傳給函式一個class型別的實參(此處是parm,型別為NS::T),為了查詢這個函式名,編譯器被要

Guru of the Week 條款21:程式碼的複雜性(第二部分)

GotW #21 Code Complexity – Part II<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 著者:Herb Sutter 翻譯:K ]

Guru of the Week 條款09:記憶體管理(上篇)

  GotW #09 Memory Management - Part I 著者:Herb Sutter 翻譯:kingofark [宣告]:本文內容取自www.gotw.ca網站上的Guru of the Week欄目,其著作權歸原著者本人所有。譯者kingofark在未經

Guru of the Week 條款20:程式碼的複雜性(第一部分)

GotW #20 Code Complexity – Part I<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 著者:Herb Sutter 翻譯:K ][

Guru of the Week 條款06:正確使用const

  GotW #06 Const-Correctness 著者:Herb Sutter 翻譯:kingofark [宣告]:本文內容取自www.gotw.ca網站上的Guru of the Week欄目,其著作權歸原著者本人所有。譯者kingofark在未經原著者本人同意的情

Guru of the Week 條款08:GotW挑戰篇——異常處理的安全性

  GotW #08 CHALLENGE EDITION Exception Safety 著者:Herb Sutter 翻譯:kingofark [宣告]:本文內容取自www.gotw.ca網站上的Guru of the Week欄目,其著作權歸原著者本人所有。譯者king

Guru of the Week 條款16:具有最大可複用性的通用Containers

GotW #16 Maximally Reusable Generic Containers<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />著者:Herb Sutter翻譯:kingofar

PyDev of the Week: Jacqueline Kazil

This week we welcome Jacqueline Kazil (@JackieKazil) as our PyDev of the Week! She is the co-author of Data Wrangling with Python. Jacqueline is the creato

NRN video of the week: Burger King taps into artificial intelligence for new ads

Burger King has launched a new campaign with the "first ads entirely created by an A.I. to air on national television," according to a Sept. 27 release fro

PyDev of the Week: K Lars Lohn

This week we welcome K Lars Lohn (@2braids) as our PyDev of the Week! He has been a part of the Python community for quite a few years. You can learn a bit

PyDev of the Week: Marc Garcia

This week we welcome Marc Garcia (@datapythonista) as our PyDev of the Week! Marc is a core developer of pandas, a Python data analysis library. If you’d l

Marginally Interesting: Tool of the week (web edition): gotapi.com

Tweet This weeks tool of the week (well, not that every week actually g

Marginally Interesting: Shell Tool of the week: rlwrap

Tweet This week’s “shell tool of the week” (and as such first of it’s k

Marginally Interesting: Tool of the Week (Ruby Edition): fastri

Tweet If you have ever consider programming in ruby, you should definit

Marginally Interesting: Tool of the Week

Tweet Recently I stumbled upon sshfs. And this is really the best thing

Marginally Interesting: Tool of the Week: baobab

Tweet I’m amazed at what useful things you can find within gnome. I acc

Interview Of The Week: Carlos Moedas

Interview Of The Week: Carlos MoedasCarlos Moedas, the European Commissioner for Research, Science, and Innovation, is a co-chair of the World Economic For

PyDev of the Week: Hillel Wayne

This week we welcome Hillel Wayne (@Hillelogram) as our PyDev of the Week! Hillel is the author of “Learn TLA+” and is currently writing “Practical TLA+” w