Phần 1
1. Goal
Learn how references provide an alternative way to access an existing object.
After this lesson, you should understand:
1. What a reference is.
2. Why references exist.
3. How references differ from pointers.
4. How pass-by-reference works.
5. Common reference mistakes.Phần 2
2. What Problem Does This Solve?
Copying objects can be expensive.
Sometimes a function must work directly with the original object.
A reference provides another name for an existing object.
Main idea:
One object
Multiple namesPhần 3
3. Minimum Syntax
Create a reference:
type& ref = object;Example:
int& ref = x;Use it exactly like the original object.
No dereferencing syntax is required.
Phần 4
4. Memory Model
Conceptually:
object ---> memoryA reference refers directly to that object.
Important:
A reference is not a new object.
A reference does not own memory.
A reference is simply another name.Phần 5
5. Reference vs Pointer
Reference:
Must bind during initialization.
Cannot be reseated.
Normally cannot be null.
Access uses normal object syntax.Pointer:
Can be null.
Can point elsewhere later.
Requires dereferencing.
Stores an address explicitly.Phần 6
6. Common Mistakes
6.1 Thinking a Reference Creates a Copy
A reference aliases an existing object.
Changes affect the original object.
6.2 Forgetting Initialization
References must be initialized immediately.
6.3 Expecting Rebinding
Once bound, a reference remains attached to the same object.
6.4 Returning a Reference to a Local Object
A local object dies when the function returns.
Returning a reference to it creates a dangling reference.
Phần 7
7. Notes for Trading Code
References are commonly used for:
order updates
market data processing
risk calculations
order book operationsBenefits:
no copy cost
simpler syntax
safer than raw pointersPhần 8
8. End-of-Day Checklist
You should be able to answer:
1. What is a reference?
2. Why use pass-by-reference?
3. How is a reference different from a pointer?
4. Can a reference be null?
5. Can a reference be rebound?
6. What is a dangling reference?Phần 9
9. Conclusion
A reference is an alias for an existing object.
The key idea:
Reference = another name for an object.References are one of the most important foundations for modern C++ and are heavily used in high-performance systems.
Mã mẫu hoàn chỉnh
cpp98_foundation/reference/main.cpp
#include <iostream>
struct Order {
int price;
int quantity;
};
void updatePrice(Order& order)
{
order.price += 1;
}
int main()
{
Order order = {100, 50};
updatePrice(order);
std::cout << "Price: "
<< order.price
<< '\n';
return 0;
}Tự kiểm tra · không chấm điểm
Bạn đã hiểu những điểm nào?
Đã tự kiểm tra 0/6 mục