C++ Reference Variable In Hindi

📔 : C++ 🔗

Reference Variable किसी existing variable का reference ही है , जिसे & operator की help से define किया जाता है। या कह सकते हैं कि reference variables किसी exist variables के alias होते हैं , फिर दोनों variables में से कोई भी use कर सकते हैं।

C++ create reference variable

string name = "Raju";
string &otherName = name;

तो कुछ इस तरह से reference variables को define करते हैं , हालाँकि इनमे से आप किसी भी variable को access करेंगे तो दोनों का output same ही आएगा।

C++ reference variable example

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;
int main() {
  string name = "Rahul Verma";
  string &otherName = name;
  
  cout << name << endl;
  cout << otherName;
  return 0;
}
Output
Rahul Verma
Rahul Verma

ध्यान रहे कि दोनों में किसी भी variable में change होने पर वो सभी दोनों variables (normal & reference variables) में भी change होगा।

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;
int main() {
  string name = "Rahul Verma";
  string &otherName = name;
  
  cout << "Before Change : " << name << endl;
  cout << otherName << endl;
  // now change value;
  name = "Other Name";
  cout << "After Change : " << name << endl;
  cout << otherName << endl;
  return 0;
  return 0;
}
Output
Before Change : Rahul Verma
Rahul Verma
After Change : Other Name
Other Name

Get Variable Address

इसके अलावा & operator का use किसी variable का memory address देखने के लिए भी किया जाता है।

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;
int main() {
  string name = "Rahul Verma";
  cout << &name;
  return 0;
}
Output
0x7ffcdfdbde80

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers