久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么通過 const 引用而不是通過值傳遞?

Why pass by const reference instead of by value?(為什么通過 const 引用而不是通過值傳遞?)
本文介紹了為什么通過 const 引用而不是通過值傳遞?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

據(jù)我所知:當(dāng)您按值傳遞時,該函數(shù)會生成傳遞參數(shù)的本地副本并使用它;當(dāng)函數(shù)結(jié)束時,它超出范圍.當(dāng)您通過 const 引用傳遞時,該函數(shù)使用對無法修改的傳遞參數(shù)的引用.然而,我不明白為什么一個人會選擇一個而不是另一個,除非在需要修改和返回參數(shù)的情況下.如果您有一個沒有返回任何內(nèi)容的 void 函數(shù),為什么要選擇一個?

From what I understand: when you pass by value, the function makes a local copy of the passed argument and uses that; when the function ends, it goes out of scope. When you pass by const reference, the function uses a reference to the passed argument that can't be modified. I don't understand, however, why one would choose one over the other, except in a situation where an argument needs to be modified and returned. If you had a void function where nothing is getting returned, why choose one over the other?

所以基本上通過常量引用傳遞避免了復(fù)制對象.那么在什么情況下復(fù)制對象好呢?我的意思是,如果一直優(yōu)化性能,為什么不一直使用 const 引用?

So basically passing by const reference avoids copying the object. So in what situations is copying the object good? I mean, why not just use const references all the time if it optimizes performance all the time?

推薦答案

有兩個主要考慮因素.一是復(fù)制傳遞對象的開銷,二是當(dāng)對象是本地對象時編譯器可以做出的假設(shè).

There are two main considerations. One is the expense of copying the passed object and the second is the assumptions that the compiler can make when the object is a a local object.

例如在第一種形式中,在 f 的主體中,不能假設(shè) ab 沒有引用同一個對象;所以 a 的值必須在寫入 b 后重新讀取,以防萬一.在第二種形式中,a 不能通過寫入 b 來改變,因為它是函數(shù)的局部變量,所以這些重新讀取是不必要的.

E.g. In the first form, in the body of f it cannot be assumed that a and b don't reference the same object; so the value of a must be re-read after any write to b, just in case. In the second form, a cannot be changed via a write to b, as it is local to the function, so these re-reads are unnecessary.

void f(const Obj& a, Obj& b)
{
    // a and b could reference the same object
}

void f(Obj a, Obj& b)
{
    // a is local, b cannot be a reference to a
}

例如:在第一個示例中,編譯器可能能夠假設(shè)本地對象的值在進行無關(guān)調(diào)用時不會更改.如果沒有關(guān)于 h 的信息,編譯器可能不知道該函數(shù)引用的對象(通過引用參數(shù))是否沒有被 h 改變.例如,該對象可能是由 h 修改的全局狀態(tài)的一部分.

E.g.: In the first example, the compiler may be able to assume that the value of a local object doesn't change when an unrelated call is made. Without information about h, the compiler may not know whether an object that that function has a reference to (via a reference parameter) isn't changed by h. For example, that object might be part of a global state which is modified by h.

void g(const Obj& a)
{
    // ...
    h(); // the value of a might change
    // ...
}

void g(Obj a)
{
    // ...
    h(); // the value of a is unlikely to change
    // ...
}

不幸的是,這個例子不是鑄鐵的.可以編寫一個類,例如,在其構(gòu)造函數(shù)中將指向自身的指針添加到全局狀態(tài)對象,以便即使是類類型的局部對象也可能被全局函數(shù)調(diào)用更改.盡管如此,對于本地對象仍然有更多機會進行有效優(yōu)化,因為它們不能被傳入的引用或其他預(yù)先存在的對象直接別名.

Unfortunately, this example isn't cast iron. It is possible to write a class that, say, adds a pointer to itself to a global state object in its constructor, so that even a local object of class type might be altered by a global function call. Despite this, there are still potentially more opportunities for valid optimizations for local objects as they can't be aliased directly by references passed in, or other pre-existing objects.

通過 const 引用傳遞參數(shù)應(yīng)該選擇在實際需要引用語義的地方,或者只有在潛在別名的成本被復(fù)制參數(shù)的成本超過時才作為性能改進.

Passing a parameter by const reference should be chosen where the semantics of references are actually required, or as a performance improvement only if the cost of potential aliasing would be outweighed by the expense of copying the parameter.

這篇關(guān)于為什么通過 const 引用而不是通過值傳遞?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 国产不卡一区 | 欧美成人一级视频 | 国产九九av| 欧美成人免费电影 | 国产 欧美 日韩 一区 | 国产女人精品视频 | 久久成人在线视频 | 亚洲精品久久久一区二区三区 | 福利影院在线看 | 99精品免费在线观看 | 亚洲精品一区二区三区四区高清 | 日韩av在线不卡 | 日韩精品一区二区三区在线 | 国产精品久久久久久av公交车 | 日韩中文在线观看 | 真人毛片 | 欧美欧美欧美 | 日韩一级免费电影 | 一区二区三区四区在线免费观看 | 97精品超碰一区二区三区 | 天天干天天玩天天操 | 免费毛片网站在线观看 | 欧美一级做a爰片免费视频 国产美女特级嫩嫩嫩bbb片 | 欧美一区二区三区电影 | 中文字幕精品一区 | 亚洲日韩中文字幕 | 欧洲一级黄 | 精品久久久久久一区二区 | 日韩欧美精品一区 | 精品成人 | 成人亚洲精品久久久久软件 | 亚州视频在线 | 久草久草久草 | 国产一级片免费视频 | www.v888av.com | 成人欧美一区二区三区白人 | 亚洲区视频 | 欧美一级免费观看 | 欧美一区二区在线观看 | 99久久精品免费看国产高清 | 亚洲综合色视频在线观看 |