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

在 C++ 中傳遞指針參數(shù),按值傳遞嗎?

Is passing pointer argument, pass by value in C++?(在 C++ 中傳遞指針參數(shù),按值傳遞嗎?)
本文介紹了在 C++ 中傳遞指針參數(shù),按值傳遞嗎?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

在 C++ 中傳遞指針參數(shù),按值傳遞嗎?因?yàn)槲铱吹綄?duì)指針的任何更改都不會(huì)反映在方法之外.我通過(guò)取消引用指針?biāo)龅母臅?huì)得到反映.

Is passing pointer argument, pass by value in C++? Since i see that any change to the pointer as such is not reflected outside the method. The changes i do by dereferencing the pointer is reflected though.

在這種情況下,使用指向指針的指針作為函數(shù)的參數(shù)來(lái)修改函數(shù)內(nèi)的指針值是否可以接受/標(biāo)準(zhǔn)程序?

In that case, is it acceptable/standard procedure to use pointer to pointer as argument to a function to modify the pointer value as such within a function?

推薦答案

是的.

指針與其他任何東西一樣按值傳遞.這意味著指針變量的內(nèi)容(指向的對(duì)象的地址)被復(fù)制.這意味著如果您更改函數(shù)體中指針的值,該更改將不會(huì)反映在仍指向舊對(duì)象的外部指針中.但是你可以改變指向的對(duì)象的值.

Pointers are passed by value as anything else. That means the contents of the pointer variable (the address of the object pointed to) is copied. That means that if you change the value of the pointer in the function body, that change will not be reflected in the external pointer that will still point to the old object. But you can change the value of the object pointed to.

如果要將指針?biāo)龅母姆从车酵獠恐羔?使其指向其他內(nèi)容),則需要兩個(gè)間接級(jí)別(指向指針的指針).當(dāng)調(diào)用函數(shù)時(shí),它是通過(guò)在指針名稱之前放置一個(gè) & 來(lái)完成的.這是標(biāo)準(zhǔn)的 C 語(yǔ)言做事方式.

If you want to reflect changes made to the pointer to the external pointer (make it point to something else), you need two levels of indirection (pointer to pointer). When calling functions it's done by putting a & before the name of the pointer. It is the standard C way of doing things.

在使用 C++ 時(shí),使用引用優(yōu)于指針(此后也使用指向指針的指針).

When using C++, using references is preferred to pointer (henceforth also to pointer to pointer).

對(duì)于為什么引用應(yīng)該優(yōu)先于指針,有幾個(gè)原因:

For the why references should be preferred to pointers, there is several reasons:

  • 引用比函數(shù)體中的指針引入更少的語(yǔ)法噪音
  • 引用保存的信息比指針多,對(duì)編譯器有用

引用的缺點(diǎn)主要是:

  • 它們打破了 C 的簡(jiǎn)單的按值傳遞規(guī)則,是什么讓理解函數(shù)關(guān)于參數(shù)的行為(它們會(huì)被改變嗎?)不太明顯.您還需要函數(shù)原型來(lái)確定.但這并不比使用 C 時(shí)所需的多個(gè)指針級(jí)別更糟糕.
  • C 不支持它們,當(dāng)您編寫(xiě)的代碼應(yīng)適用于 C 和 C++ 程序時(shí),這可能會(huì)成為一個(gè)問(wèn)題(但這不是最常見(jiàn)的情況).

在指針到指針的特定情況下,區(qū)別主要是簡(jiǎn)單,但使用引用可能也很容易刪除兩級(jí)指針,只傳遞一個(gè)引用而不是指向指針的指針.

In the specific case of pointer to pointer, the difference is mostly simplicity, but using reference it may also be easy to remove both levels of pointers and pass only one reference instead of a pointer to pointer.

這篇關(guān)于在 C++ 中傳遞指針參數(shù),按值傳遞嗎?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(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++ 中,為什么我不能像這樣編寫(xiě) 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)形?)
主站蜘蛛池模板: 日本精品久久久一区二区三区 | 国产欧美日韩一区二区三区在线观看 | 国产免费一区二区三区 | 91国产精品在线 | 美女黄网站 | 狠狠干综合视频 | 中文字幕二区 | 在线视频亚洲 | 国产蜜臀97一区二区三区 | 新91视频网 | 亚洲二区视频 | 精品在线免费观看视频 | 嫩草影院网址 | 国产精品久久久久久久久久三级 | 日韩一区二区久久 | 欧美一区二区激情三区 | 国产成人精品一区二区三区在线 | 欧美激情一区二区三区 | 美女黄频 | 久久精品a级毛片 | 欧美激情一区 | 91在线看 | 免费精品视频在线观看 | 久久中文字幕一区 | 日本一区二区三区免费观看 | 中文字幕在线观看 | 日韩国产高清在线观看 | 天天拍天天操 | 久久99国产精品久久99果冻传媒 | 在线视频亚洲 | av免费电影在线 | 北条麻妃视频在线观看 | 欧美精品在欧美一区二区 | 在线观看av网站 | www.亚洲视频.com | 97伦理电影 | 国产在线精品一区二区 | 成人在线视频一区 | 在线色网| 亚洲不卡视频 | 成人亚洲 |