Ugrás a tartalomhoz

Szerkesztő:LinguisticMystic/cpp/References

A Wikiszótárból, a nyitott szótárból

🔁 What is a Reference?

[szerkesztés]

In C++, a reference is essentially an alias for another variable. It does not hold its own memory; instead, it refers directly to an already existing object.

Think of a reference as a nickname for a variable. Any operations performed on the reference are actually performed on the original variable.



🛠️ Declaration and Initialization

[szerkesztés]

You declare a reference by placing an ampersand (&) after the type:

int a = 10;
int &b = a;  // 'b' is now an alias for 'a'

❗Important Rules:

[szerkesztés]
  • References must be initialized when declared.
  • You cannot rebind a reference to another variable later.
  • Once bound, a reference is permanently tied to its original variable.



🧪 Example: Reference in Action

[szerkesztés]
#include <iostream>
using namespace std;

int main() {
    int number = 42;
    int &ref = number; // 'ref' is a reference (alias) to 'number'
    
    cout << "Value of number: " << number << endl;
    cout << "Value of ref: " << ref << endl;
    
    ref = 99;  // Modifying ref changes number

    cout << "New value of number: " << number << endl;
    cout << "New value of ref: " << ref << endl;

    return 0;
}

🔍 Code Explanation:

[szerkesztés]
  • int number = 42;: Declare a variable.
  • int &ref = number;: Declare a reference named ref, which is now tied to number.
  • ref = 99;: Since ref is an alias for number, this updates number to 99.

Output:

Value of number: 42
Value of ref: 42
New value of number: 99
New value of ref: 99

🔁 Reference vs Pointer

[szerkesztés]
Feature Reference Pointer
Syntax int &r = var; int *p = &var;
Null allowed? ❌ Cannot be null ✅ Can be null
Must be initialized? ✅ Must be initialized ❌ Optional
Can rebind? ❌ No rebinding ✅ Can point to different objects
Dereferencing needed? ❌ No (ref directly accesses value) ✅ Yes (*ptr to get value)
Memory footprint 🔒 Reference is not a real object 🧠 Pointer occupies memory
Use case Safer, simpler access More flexible, lower-level operations



⚙️ Pointers vs References in Practice

[szerkesztés]
int number = 13;
int *ptr = &number; // Pointer: stores address
int &ref = number;  // Reference: alias for 'number'
  • *ptr = 50; → modifies number through pointer.
  • ref = 50; → modifies number through reference.

Under the hood, a reference is often implemented as a constant pointer, but the compiler ensures it behaves in a safer, more predictable way.



✅ When to Use a Reference

[szerkesztés]

Use a reference when:

  • You want to pass a variable to a function without copying it.
  • You want a function to modify the original variable.
  • You want cleaner syntax than pointers (no * or & dereferencing).



🧠 Conclusion

[szerkesztés]

C++ references offer a simpler and safer alternative to pointers for indirect access:

  • They must be initialized and cannot be reseated.
  • They act like the original variable, simplifying code.
  • Use them when safety, readability, and clarity are more important than flexibility.

Mastering references is key to writing efficient and elegant C++ programs, especially for function parameters, operator overloading, and class member access.