C++ के References
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Pass by Reference
आप functions को arguments reference से pass कर सकते हैं, जो बड़े variables या objects copy करने से बचता है और function को caller के original arguments सीधे update करने देता है।
यह typically एक primitive type से बड़ी किसी भी चीज़ के लिए value से pass करने से तेज़ है।
उदाहरण: Pass by Reference
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Constant References
अगर आप function को आपका data modify करने दिए बिना reference passing का performance benefit चाहते हैं, एक const reference pass करें। यह बड़े objects copy करने की cost avoid करता है फिर भी guarantee करते हुए कि caller की original value untouched रहे।
उदाहरण: Constant References
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक reference को बिना initialize किए declare करना, जैसे
int& r;, जो एक compile error है क्योंकि एक reference को declaration पर bind होना ज़रूरी है। - यह उम्मीद करना कि
r = y;reference कोyrefer कराएगा, जब यहyकी value को original variable में copy कर देता है। - एक local variable का reference return करना, जो function खत्म होने के बाद exist नहीं करता।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: