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

關于 C++ 中指針和引用的混淆

Confusion about pointers and references in C++(關于 C++ 中指針和引用的混淆)
本文介紹了關于 C++ 中指針和引用的混淆的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我有一堆這樣的代碼:

#include <iostream>
using namespace std;

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int a;
    int b;
    a = 7;
    b = 5;
    swap(a, b);
    cout << a << b;

    return 0;
}

這段代碼按照我真正想要交換 2 個數字的方式執行交換過程,但是當我想要用戶提供兩個數字時,如下所示;

This code does the swapping process as what I exactly wanted to swap 2 numbers, but when I want two numbers from the user as follows;

int a;
int b;
cin >> a;
cin >> b;
swap(a, b);
cout << a << b;

編譯器給了我一個關于 intint* 錯誤的錯誤,這是預期的.盡管我沒有使用帶有 & 運算符的方法,為什么第一個代碼會進行正確的交換?

the compiler gives me an error about int to int* error which is as expected. Why does the first code do the right swapping although I didn't use the method with & operator?

推薦答案

在第一個例子中,std::swap 被調用,因為你的using namespace std.第二個例子和第一個完全一樣,所以你可能沒有用.

In the first example, std::swap is called, because of your using namespace std. The second example is exactly the same as the first one, so you might have no using.

無論如何,如果您將函數重命名為 my_swap 或類似名稱(并更改每次出現的次數),那么第一個代碼不應按預期工作.或者,刪除 using namespace std 并顯式調用 std::cinstd::cout.我會推薦第二個選項.

Anyway, if you rename your function to my_swap or something like that (and change every occurence), then the first code shouldn't work, as expected. Or, remove the using namespace std and call std::cin and std::cout explicitly. I would recommend the second option.

這篇關于關于 C++ 中指針和引用的混淆的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 亚洲精品中文字幕在线观看 | 国产精品久久久久久久久久免费 | 欧美一区二区三区电影 | 成人久久久久 | 日韩欧美一级片 | 欧美淫片 | 日日操操 | 日韩亚洲欧美一区 | 国产在视频一区二区三区吞精 | 国产成人a亚洲精品 | 欧美福利影院 | 国产在线小视频 | 求个av网址| 国产精品久久久久久久久久久久久久 | 请别相信他免费喜剧电影在线观看 | 最新中文字幕在线 | 亚洲视频免费在线 | 国产精品成人一区二区三区 | 中国一级特黄真人毛片免费观看 | 人人人人干 | 亚洲国产成人精品女人久久久 | 国产精品揄拍一区二区久久国内亚洲精 | 91九色porny首页最多播放 | 亚洲精品成人 | 日本三级全黄三级a | 亚洲一区精品在线 | 久久国产日韩欧美 | 日本午夜在线视频 | 精品一区二区三区免费视频 | 精品欧美一区二区三区久久久 | 久色| 第一色在线 | 久久亚洲精品久久国产一区二区 | 天堂久久一区 | 狠狠撸在线视频 | 欧美精品成人 | 视频一区二区中文字幕 | 福利社午夜影院 | 精品免费国产一区二区三区四区 | 99国产精品99久久久久久 | 91性高湖久久久久久久久_久久99 |