搜尋此網誌

2025年6月29日星期日

Reference & in C++

In C++, a reference is essentially an alias for another variable. Once a reference is initialized to a variable, it becomes just another name for that variable—any operation on the reference is actually performed on the original variable.

- Declared using the & symbol

int a = 10;
int& ref = a;  // ref is a reference to a

- Must be initialized when declared.
- Cannot be changed to refer to another variable after initialization.
- Useful for function arguments and return values to avoid copying large data.

Reference:
int& ref = var;
cannot be null
cannot be reassigned
do not need dereferencing

Pointer:
int* ptr = &var;
can be null
can point to difference variables
need dereferencing

In programming, an alias is a second name for the same memory location. It means two or more variables refer to the same underlying data—so changing one affects the other.

Modifying a reference does change the original variable, because they both refer to the same memory location.

In programming, nullability refers to whether a variable is allowed to hold a null value --- meaning it can represent the absence of a value.

syntax: (computing) the rules that state how words and phrases must be used in a computer language

In C++, a reference is essentially an alias for another variable. That means:
- It doesn't exist as a separate object in memory.
- When you take the address of a reference, you're actually getting the address of the original variable it refers to.

C++ does not allow arrays of references.

References are not objects: They don’t occupy their own memory --- they’re just aliases for existing variables.

Arrays require elements to be assignable: But references must be initialized when declared and cannot be reseated.

References must be initialized at declaration.

A reference is not a standalone object; it must alias an existing variable.
Because of this, the compiler needs to know what it's referring to immediately --- there is no such thing as a "null" or "unbound" reference.

In C++, pointer initialization is optional, but that comes with a big caveat.

caveat: a warning that particular things need to be considered before something can be done

In C++, references provide one level of indirection.

indirection: indirect action or procedure

In C++, multiple levels of indirection with pointers means having pointers that point to other pointers --- and this can go as deep as your brain (or compiler) can handle!

When you use a reference:
- You're not accessing the object directly.
- Instead, you're accessing it through an alias --- a single level removed from the actual object.

In C++, you can absolutely declare a pointer with the void type, and it's known as a void pointer or generic pointer.

A void* is a special type of pointer that can point to any data type, but it doesn't know what type it's pointing to.

References in C++ are like secret passageways --- they let you access and manipulate data efficiently without the overhead of copying.

iteration: the process of repeating a mathematical or computing process or set of instructions again and again, each time applying it to the result of the previous stage

Microsoft Copilot
www.oxfordlearnersdictionaries.com

沒有留言:

發佈留言