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

通過指針&通過引用傳遞

Pass by pointer amp; Pass by reference(通過指針amp;通過引用傳遞)
本文介紹了通過指針&通過引用傳遞的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

可能的重復:
指針變量和指針變量有什么區別C++中的引用變量?
路過有好處嗎在 C++ 中通過引用傳遞指針?

在這兩種情況下,我都達到了結果.那么什么時候一個比另一個更受歡迎呢?我們使用一個而不是另一個的原因是什么?

In both cases, I achieved the result. So when is one preferred over the other? What are the reasons we use one over the other?

#include <iostream>
using namespace std;
void swap(int* x, int* y)
{
    int z = *x;
    *x=*y;
    *y=z;
}
void swap(int& x, int& y)
{
    int z = x;
    x=y;
    y=z;
}

int main()
{
    int a = 45;
    int b = 35;
    cout<<"Before Swap
";
    cout<<"a="<<a<<" b="<<b<<"
";

    swap(&a,&b);
    cout<<"After Swap with pass by pointer
";
    cout<<"a="<<a<<" b="<<b<<"
";

    swap(a,b);
    cout<<"After Swap with pass by reference
";
    cout<<"a="<<a<<" b="<<b<<"
";
}

輸出

Before Swap
a=45 b=35
After Swap with pass by pointer
a=35 b=45

After Swap with pass by reference
a=45 b=35

推薦答案

引用在語義上如下:

T&<=>*(T * const)

const T&<=>*(T const * const)

T&&<=>[無 C 等價物] (C++11)

與其他答案一樣,C++ 常見問題解答中的以下內容是單行答案:可能時引用,需要時使用指針.

As with other answers, the following from the C++ FAQ is the one-line answer: references when possible, pointers when needed.

優于指針的一個優點是您需要顯式轉換才能傳遞 NULL.不過還是有可能的.在我測試過的編譯器中,沒有一個會發出以下警告:

An advantage over pointers is that you need explicit casting in order to pass NULL. It's still possible, though. Of the compilers I've tested, none emit a warning for the following:

int* p() {
    return 0;
}
void x(int& y) {
  y = 1;
}
int main() {
   x(*p());
}

這篇關于通過指針&amp;通過引用傳遞的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 日美女逼逼 | 免费成人国产 | 精精国产xxxx视频在线 | 欧美国产日韩在线观看成人 | a看片| 日韩一区二区av | 521av网站 | 午夜影院在线观看版 | www.日韩系列 | www.操com | 国产日韩一区二区三区 | 神马影院一区二区三区 | 一区二区免费 | 国产一二区视频 | 欧美精品乱码久久久久久按摩 | 狠狠操天天操 | 精品一区二区电影 | 久久久久久久久久久久久九 | 国产精品成人久久久久 | 久草视频在 | 国产在线精品一区二区三区 | 日本午夜在线视频 | 国产精品久久久久久久免费大片 | 天天操夜夜拍 | 色呦呦在线 | 成人毛片在线观看 | 激情免费视频 | 久久高清免费视频 | 自拍偷拍亚洲视频 | 亚洲成人精品久久久 | 国产精品性做久久久久久 | 一区二区三区视频在线 | jdav视频在线观看免费 | 国产精品第2页 | 日韩在线观看中文字幕 | 久久精品日产第一区二区三区 | 精品国产伦一区二区三区观看方式 | 欧美精品久久久 | 精品一区二区三区在线观看 | 免费一级黄色电影 | 亚洲国产情侣 |