← Back to C++ Course | Chapter 7: Pointers & References | Lesson 7 of 11

C++ के References

एक reference same variable का एक दूसरा नाम है, एक व्यक्ति के nickname की तरह। एक नाम से इसे बदलना दोनों के लिए इसे बदलता है।
Syntax
cpp
data_type &reference_name = variable_name;  // an alias for variable_name

Reference क्या है?

C++ में एक reference एक existing variable का alias है, original के साथ exact same memory address share करते हुए, इसलिए एक बार बनने के बाद, reference के जरिए किया गया कोई भी change सीधे उस variable को update करता है जिसे यह refer करता है।

एक pointer के उलट, एक reference को बाद में किसी और चीज़ को refer करने के लिए reseat नहीं किया जा सकता।

उदाहरण: What is a Reference?

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare value and set it to 5
	int value = 5;
	int &ref = value;
	ref = 10;
	// Print to the console with cout
	std::cout << value << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

References के जरिए Variables Modify करना

क्योंकि एक reference अपने target की memory location share करता है, reference को modify करना original variable को modify करने से indistinguishable है — sync में रखने के लिए कोई अलग copy नहीं है।

यह references को useful बनाता है जब भी आप same piece of data के लिए एक alternate नाम चाहते हों।

उदाहरण: Modifying Variables via References

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare score and set it to 50
	int score = 50;
	int &scoreRef = score;
	scoreRef += 20;
	// Print to the console with cout
	std::cout << score << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Pass by Reference

आप functions को arguments reference से pass कर सकते हैं, जो बड़े variables या objects copy करने से बचता है और function को caller के original arguments सीधे update करने देता है।

यह typically एक primitive type से बड़ी किसी भी चीज़ के लिए value से pass करने से तेज़ है।

उदाहरण: Pass by Reference

cpp
// Include std::cout and std::cin
#include <iostream>

void increase(int &x) {
	x += 1;
}

// Program execution starts in main()
int main() {
	// Declare num and set it to 5
	int num = 5;
	increase(num);
	// Print to the console with cout
	std::cout << num << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Constant References

अगर आप function को आपका data modify करने दिए बिना reference passing का performance benefit चाहते हैं, एक const reference pass करें। यह बड़े objects copy करने की cost avoid करता है फिर भी guarantee करते हुए कि caller की original value untouched रहे।

उदाहरण: Constant References

cpp
#include <iostream>
#include <string>

void printName(const std::string &name) {
	std::cout << name << std::endl;
	// name = "Changed"; // compile error
}

int main() {
	printName("Alex");
	return 0;
}

Functions से References Return करना

Functions references return कर सकते हैं, जो method calls chain करने या एक container के अंदर एक modifiable slot expose करने के लिए useful है।

कभी एक local variable का reference return न करने के लिए careful रहें, क्योंकि यह function के return होते ही dangle होगा और invalid memory की ओर point करेगा।

उदाहरण: Returning References from Functions

cpp
// Include std::cout and std::cin
#include <iostream>

int scores[3] = {10, 20, 30};

int &getScore(int index) {
	// Send scores[index] back to the caller
	return scores[index];
}

// Program execution starts in main()
int main() {
	getScore(1) = 99;
	// Print to the console with cout
	std::cout << scores[1] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक reference को बिना initialize किए declare करना, जैसे int& r;, जो एक compile error है क्योंकि एक reference को declaration पर bind होना ज़रूरी है।
  2. यह उम्मीद करना कि r = y; reference को y refer कराएगा, जब यह y की value को original variable में copy कर देता है।
  3. एक local variable का reference return करना, जो function खत्म होने के बाद exist नहीं करता।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.