संभव डुप्लिकेट:
What are the differences between pointer variable and reference variable in C++?एम्पर्सेंड (&) साइन सी ++ में कैसे काम करता है?
यह मैं भ्रामक:
class CDummy
{
public:
int isitme (CDummy& param);
};
int CDummy::isitme (CDummy& param)
{
if (¶m == this)
{
return true; //ampersand sign on left side??
}
else
{
return false;
}
}
int main()
{
CDummy a;
CDummy* b = &a;
if (b->isitme(a))
{
cout << "yes, &a is b";
}
return 0;
}
सी & में आम तौर पर एक वर का पता होता है। इसका मतलब क्या है? क्या यह पॉइंटर नोटेशन का एक शानदार तरीका है? कारण यह है कि मैं यह मान रहा हूं कि यह एक सूचक संकेत है क्योंकि यह सब के बाद एक सूचक है और हम दो पॉइंटर्स की समानता की जांच कर रहे हैं। धन्यवाद।
1) एक चर
int x;
void* p = &x;
//p will now point to x, as &x is the address of x
2) का पता लेने के एक समारोह
void foo(CDummy& x);
//you pass x by reference
//if you modify x inside the function, the change will be applied to the original variable
//a copy is not created for x, the original one is used
//this is preffered for passing large objects
//to prevent changes, pass by const reference:
void fooconst(const CDummy& x);
3) के संदर्भ द्वारा एक तर्क पारित:
कोई अनादर के इरादे से, यह किसी भी प्रारंभिक सी ++ पुस्तक के पहले कुछ अध्यायों में शामिल होगा। क्या मेरा सुझाव है कि आप एक से परामर्श लें? –
अच्छी तरह से मैं cplusplus.com से पढ़ रहा हूं और उनके पास यह उदाहरण है। – infinitloop
"* मैं cplusplus.com से पढ़ रहा हूं *" यह आपकी पहली गलती है। साइट एक अच्छा संदर्भ नहीं है, और ** विशेष रूप से ** एक अच्छा सीखने संसाधन नहीं है। – ildjarn