References
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
A C++ reference is a second name for an object that already exists: it makes no new box of memory. Follow a ten-line program as int& ref = score puts a second name tag on score's box, a write through ref changes score, and a reference parameter lets a function double the caller's variable. Then drop the & to see pass by value make a copy, and meet const& for reading big values without copying them.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01After int score = 5; int& ref = score; ref = 7; what is score?
ref is another name for score's box. Writing 7 through ref writes it into that one box, so score is 7.
02What new memory does int& ref = score; create?
A reference is not a new variable. It is bound to an existing object, so &ref == &score. (How the compiler stores a reference, if at all, is up to it; a local reference usually needs no memory.)
03Which line does not compile?
A reference must be bound when it is made, so int& r; is a compile error. const int& r = 5; is fine: a const reference may bind to a literal.
04void twice(int n) { n = n * 2; } is called as twice(score) with score = 14. What is score afterwards?
Without the &, n is a copy of score in twice's own frame. Doubling it changes only the copy (to 28); score stays 14.
05A function only needs to read a large std::string. Which parameter is the usual choice?
const std::string& passes no copy and promises not to change the string (C++ Core Guidelines F.16). Plain & is for changing the caller's object; by value copies it.
المسرد
- Reference
- A second name for an object that already exists, written with & after the type: int& r = x;
- Alias
- Another name for the same thing. A reference is an alias for the object it is bound to.
- Bind
- To attach a reference to its object. It happens once, when the reference is made, and never changes.
- Stack frame
- The memory a function call gets for its local variables and parameters; it is removed when the function returns.
- Pass by value
- The parameter is a copy of the argument; changing it does not change the caller's variable.
- Pass by reference
- The parameter is a reference bound to the argument; no copy is made and changes reach the caller's variable.
- const reference
- A reference you can only read through (const T&). It avoids a copy and cannot change the object.
- Dangling reference
- A reference to an object that no longer exists; using one is undefined behaviour.
الملاحظات والمصادر
- Addresses such as 0x1c are short made-up examples. Real stack addresses are much longer and change from run to run.
- The C++ standard does not say how a reference is stored: a reference is not an object and need not take any memory. In practice a reference parameter such as int& n is usually passed as the address of the argument, much like a pointer.
- A reference must not outlive the box it names. Returning a reference to a local variable leaves it dangling, and using a dangling reference is undefined behaviour.
- Compared with a pointer: no * to read through it, no & at the call site, never null and never re-seated. When you need 'no object' or to point somewhere else later, use a pointer.
- int& r; is a compile error (GCC: "'r' declared as reference but not initialized"). A non-const reference cannot bind to a literal: int& r = 5; does not compile, while const int& r = 5; does.
- The program on the card is real, standard C++. It compiles with no warnings under g++ or clang++ -std=c++20 -Wall -Wextra, and ./refs exits with status 14.
اختبار يتاح بعد الخطوة الأخيرة.