Phần 1
1. Goal
After learning nullptr, you should understand:
1. Why C++11 introduced `nullptr`.
2. How to use `nullptr` correctly.
3. Why `nullptr` is safer than `NULL` or `0`.
4. Common mistakes related to null pointers.
5. How `nullptr` improves readability in real-world C++ code.Phần 2
2. What Problem Does nullptr Solve?
Before C++11, programmers often used NULL or 0 to represent a null pointer:
int* p = NULL;
int* q = 0;The problem is that NULL is usually just an integer constant, often defined as 0.
This can cause ambiguity, especially with function overloading.
nullptr solves this problem by introducing a real null pointer value with its own type:
std::nullptr_tMain idea:
nullptr clearly means: this is a null pointer, not an integer.Phần 3
3. Minimum Syntax
Initialize a pointer with no target:
int* p = nullptr;Check whether a pointer is null:
if (p == nullptr) {
// p does not point to anything
}Reset a pointer:
p = nullptr;Pass a null pointer to a function:
processOrder(nullptr);Phần 4
4. Why nullptr Is Better Than NULL
4.1. NULL can behave like an integer
f(NULL);If there are overloaded functions, this may call the wrong overload.
Example idea:
f(int)
f(int*)
NULL may match f(int), because NULL is often just 0.4.2. nullptr is type-safe
f(nullptr);This clearly means the pointer overload should be used.
Summary:
NULL = old style, may behave like 0
0 = integer literal
nullptr = modern, type-safe null pointerPhần 5
5. Common Mistakes
5.1. Dereferencing nullptr
int* p = nullptr;
*p = 10;This is dangerous because p does not point to valid memory.
Rule:
Always check a pointer before dereferencing it.Correct idea:
if (p != nullptr) {
*p = 10;
}5.2. Using NULL in modern C++
Old style:
Order* order = NULL;Modern C++ style:
Order* order = nullptr;Use nullptr whenever you mean "no pointer".
5.3. Thinking nullptr is the same as 0
nullptr can convert to pointer types, but it is not an integer.
int* p = nullptr; // OK
int x = nullptr; // Error5.4. Forgetting to check for null
A function may return nullptr when it cannot find a valid object.
Order* order = findOrder(id);Before using it, check:
if (order != nullptr) {
// safe to use order
}Otherwise, accessing order->price may crash the program.
Phần 6
6. When Should You Use nullptr?
Use nullptr when:
1. Initializing a pointer with no target.
2. Resetting a pointer.
3. Checking whether a pointer is null.
4. Passing a null pointer to a function.
5. Writing modern C++ instead of old C-style code.Phần 7
7. Notes for Trading / Performance-Sensitive Code
In trading systems, pointers may refer to objects such as:
Order
Trade
MarketData
OrderBook
ConnectionA null pointer may mean:
1. No active order.
2. No valid market data.
3. No current connection.
4. No available order book.Using nullptr makes the intention clear:
There is no valid object here.This is safer and more readable than using NULL or 0.
Phần 8
8. End-of-Day Checklist
You should remember:
nullptr = a real null pointer value
NULL = usually just 0
0 = an integer literalYou should be able to answer:
1. Why is `nullptr` safer than `NULL`?
2. What type does `nullptr` have?
3. What happens if you dereference `nullptr`?
4. Why can `NULL` cause overload ambiguity?
5. When should you use `nullptr` in modern C++?Phần 9
9. Conclusion
nullptr is a small but important C++11 feature.
The key idea:
Use nullptr whenever you mean "no pointer".
Do not use NULL or 0 in modern C++ pointer code.:::
Mã mẫu hoàn chỉnh
cpp11/2_nullptr/main.cpp
#include <iostream>
#include <vector>
#include <string>
struct Order {
int id;
std::string symbol;
double price;
int quantity;
};
Order* findOrderById(std::vector<Order>& orders, int target_id) {
for (auto& order : orders) {
if (order.id == target_id) {
return ℴ
}
}
// Không tìm thấy order nào
return nullptr;
}
void printOrder(const Order* order) {
if (order == nullptr) {
std::cout << "Order not found\n";
return;
}
std::cout << "Order ID: " << order->id << '\n';
std::cout << "Symbol: " << order->symbol << '\n';
std::cout << "Price: " << order->price << '\n';
std::cout << "Quantity: " << order->quantity << '\n';
}
int main() {
std::vector<Order> orders = {
{101, "AAPL", 191.25, 100},
{102, "MSFT", 420.50, 50},
{103, "NVDA", 875.10, 10}
};
Order* order1 = findOrderById(orders, 102);
printOrder(order1);
std::cout << "----\n";
Order* order2 = findOrderById(orders, 999);
printOrder(order2);
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/5 mục