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

在 C++ 中通過(guò)引用傳遞對(duì)象

passing object by reference in C++(在 C++ 中通過(guò)引用傳遞對(duì)象)
本文介紹了在 C++ 中通過(guò)引用傳遞對(duì)象的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

在 C++(也包括 C)中通過(guò)引用傳遞變量的常用方法如下:

The usual way to pass a variable by reference in C++(also C) is as follows:

void _someFunction(dataType *name){ // dataType e.g int,char,float etc.
/****
definition
*/
}

int main(){
    dataType v;
    _somefunction(&v);  //address of variable v being passed
    return 0;
}

但令我驚訝的是,我注意到當(dāng)通過(guò)引用傳遞一個(gè)對(duì)象時(shí),對(duì)象的名稱(chēng)本身就起到了作用(不需要 & 符號(hào)),并且在聲明/函數(shù)定義 參數(shù)前不需要*符號(hào).下面的例子應(yīng)該很清楚:

But to my surprise, I noticed when passing an object by reference the name of object itself serves the purpose(no & symbol required) and that during declaration/definition of function no * symbol is required before the argument. The following example should make it clear:

// this
#include <iostream>
using namespace std;

class CDummy {
  public:
    int isitme (CDummy& param);     //why not (CDummy* param);
};

int CDummy::isitme (CDummy& param)
{
  if (&param == this) return true;
  else return false;
}

int main () {
  CDummy a;
  CDummy* b = &a;
  if ( b->isitme(a) )               //why not isitme(&a)
    cout << "yes, &a is b";
  return 0;
}

我無(wú)法理解為什么要對(duì) class 進(jìn)行這種特殊處理.即使是幾乎像一個(gè)類(lèi)的結(jié)構(gòu)也不是這樣使用的.對(duì)象名稱(chēng)是否和數(shù)組一樣被視為地址?

I have problem understanding why is this special treatment done with class . Even structures which are almost like a class are not used this way. Is object name treated as address as in case of arrays?

推薦答案

讓您感到困惑的是,聲明為傳遞引用的函數(shù)(使用 &) 不使用實(shí)際地址調(diào)用,即 &a.

What seems to be confusing you is the fact that functions that are declared to be pass-by-reference (using the &) aren't called using actual addresses, i.e. &a.

簡(jiǎn)單的答案是將函數(shù)聲明為傳遞引用:

The simple answer is that declaring a function as pass-by-reference:

void foo(int& x);

就是我們所需要的.然后它會(huì)自動(dòng)通過(guò)引用傳遞.

is all we need. It's then passed by reference automatically.

你現(xiàn)在像這樣調(diào)用這個(gè)函數(shù):

You now call this function like so:

int y = 5;
foo(y);

y 將通過(guò)引用傳遞.

and y will be passed by reference.

你也可以這樣做(但為什么要這樣做?咒語(yǔ)是:盡可能使用引用,需要時(shí)使用指針):

You could also do it like this (but why would you? The mantra is: Use references when possible, pointers when needed) :

#include <iostream>
using namespace std;

class CDummy {
public:
    int isitme (CDummy* param);
};


int CDummy::isitme (CDummy* param)
{
    if (param == this) return true;
    else return false;
}

int main () {
    CDummy a;
    CDummy* b = &a;             // assigning address of a to b
    if ( b->isitme(&a) )        // Called with &a (address of a) instead of a
        cout << "yes, &a is b";
    return 0;
}

輸出:

yes, &a is b

這篇關(guān)于在 C++ 中通過(guò)引用傳遞對(duì)象的文章就介紹到這了,希望我們推薦的答案對(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)文檔推薦

What do compilers do with compile-time branching?(編譯器如何處理編譯時(shí)分支?)
Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 嗎?)
Checking for NULL pointer in C/C++(在 C/C++ 中檢查空指針)
Math-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比較運(yùn)算符的數(shù)學(xué)式鏈接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之間的區(qū)別與“if())
C++, variable declaration in #39;if#39; expression(C++,if 表達(dá)式中的變量聲明)
主站蜘蛛池模板: 欧美福利视频一区 | 日韩中文字幕在线观看视频 | 日韩精品一区二区三区视频播放 | 国产精品免费一区二区三区 | 精品一区二区三区中文字幕 | 亚洲97 | 91精品无人区卡一卡二卡三 | 91精品国产91久久综合桃花 | 亚洲视频一区在线观看 | 一区二区精品视频 | 久久久久成人精品免费播放动漫 | 国产探花在线观看视频 | 欧美精品一区二区三区在线播放 | 成年免费大片黄在线观看一级 | 激情欧美日韩一区二区 | 国产精品欧美大片 | 国产激情视频网 | 国产亚洲第一页 | 欧美一级欧美三级在线观看 | 久久一区二区精品 | 久草日韩 | 中文字幕精品一区二区三区在线 | 亚洲一区二区久久 | 亚洲日韩中文字幕一区 | 在线观看视频91 | 羞羞的视频在线看 | 欧美黄色录像 | 亚洲成色777777在线观看影院 | 一区在线播放 | 国内av在线 | 久久精品女人天堂av | 美日韩免费视频 | 在线观看免费毛片 | 正在播放一区二区 | 男女在线网站 | 精品久久精品 | 国产综合久久 | 国产精品欧美精品日韩精品 | 天堂网avav | 中文精品久久 | 国产成人99久久亚洲综合精品 |