Phần 1
1. Definition
A deleted function is a function that is explicitly disabled by the programmer.
The function is declared, but the compiler is told that it must not be used.
Phần 2
2. Why It Was Introduced
Before C++11, programmers often used private declarations to prevent certain operations such as copying.
That technique was indirect and could produce unclear error messages.
C++11 introduced deleted functions to express the intention directly.
The programmer can clearly state that a function exists as part of the interface, but using it is forbidden.
Phần 3
3. Main Idea
A deleted function means:
This operation is intentionally not allowed.
It is commonly used to prevent copying, assignment, or unwanted overloads.
Phần 4
4. Common Use Cases
Deleted functions are commonly used to:
Disable copying
Disable assignment
Prevent accidental type conversions
Block unsafe overloads
Make ownership rules explicit
Phần 5
5. Deleted Function vs Missing Function
A missing function simply does not exist.
A deleted function exists in the interface, but any attempt to use it causes a compile-time error.
This makes the programmer's intention clearer and gives better diagnostics.
Phần 6
6. Deleted Function vs Defaulted Function
A defaulted function asks the compiler to generate its normal implementation.
A deleted function tells the compiler that the function must not be used.
They are opposite tools.
Defaulted functions enable compiler-generated behavior.
Deleted functions disable specific behavior.
Phần 7
7. Compile-Time Protection
Deleted functions create errors at compile time, before the program runs.
This is useful because unsafe or unwanted operations are rejected early.
The program cannot accidentally perform the disabled operation at runtime.
Phần 8
8. Common Mistakes
Mistake 1: Thinking It Deletes an Object
A deleted function does not delete memory or destroy an object.
It only disables a function from being called.
Mistake 2: Confusing It with Defaulted Functions
Defaulted functions request compiler-generated behavior.
Deleted functions forbid usage.
Mistake 3: Deleting a Function That Is Actually Needed
If normal code needs copying, assignment, or another operation, deleting it will make the type harder to use.
Mistake 4: Assuming Runtime Behavior
Deleted functions are checked by the compiler.
They are not runtime checks.
Phần 9
9. When to Use It
Use deleted functions when:
An operation would be unsafe.
An operation does not make sense for the type.
A class owns a resource and should not be copied.
You want compile-time protection against accidental usage.
You want the interface to clearly communicate forbidden behavior.
Phần 10
10. Benefits
Makes forbidden operations explicit.
Produces compile-time errors.
Improves class interface clarity.
Helps enforce ownership rules.
Prevents accidental copying or conversion.
Phần 11
11. End-of-Day Checklist
You should be able to explain:
What a deleted function means.
Why C++11 introduced deleted functions.
How deleted functions differ from defaulted functions.
Why deleted functions are checked at compile time.
Why deleted functions are useful for resource-owning classes.
Why deleting a function is not the same as deleting memory.
Phần 12
12. Conclusion
Deleted functions explicitly disable operations that should not be used.
They are a compile-time safety tool for making class interfaces clearer and preventing invalid behavior.
Mã mẫu hoàn chỉnh
cpp11/15_delete function/main.cpp
#include <iostream>
class OrderManager {
public:
OrderManager() = default;
OrderManager(const OrderManager&) = delete;
OrderManager& operator=(const OrderManager&) = delete;
void submitOrder(int quantity) {
std::cout << "Submit order quantity = " << quantity << '\n';
}
};
int main() {
OrderManager manager;
manager.submitOrder(100);
// Uncomment dòng này sẽ lỗi compile:
// OrderManager copy = manager;
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