Phần 1
1. Goal
After learning defaulted functions, you should understand:
1. What = default asks the compiler to do.
2. Why an explicitly defaulted constructor is useful.
3. Which special member functions can be defaulted.
4. The difference between = default and an empty function body.
5. Common mistakes when using = default.Phần 2
2. What Problem Does It Solve?
C++ can automatically generate several special member functions for a class.
For example, an empty class can be created without writing a constructor:
class Order {
};
Order order;However, after declaring another constructor, the compiler no longer automatically provides a constructor with no parameters.
class Order {
public:
Order(int id) : id(id) {}
private:
int id;
};This no longer works:
Order order;C++11 allows us to explicitly request the compiler-generated constructor:
class Order {
public:
Order() = default;
Order(int id) : id(id) {}
private:
int id{};
};Main idea:
= default explicitly asks the compiler to generate the normal implementation.Phần 3
3. Minimum Syntax
class Order {
public:
Order() = default;
};Read it as:
Generate the default constructor for Order.Phần 4
4. Which Functions Can Use = default?
= default is used with special member functions:
class Order {
public:
Order() = default;
~Order() = default;
Order(const Order&) = default;
Order& operator=(const Order&) = default;
};For now, the most important case is:
Order() = default;The other forms become more useful when studying object copying, assignment, and resource management.
Phần 5
5. = default vs an Empty Body
These two constructors may appear similar:
Order() = default;Order() {}They are not exactly the same.
= default requests the compiler's normal generated constructor.
An empty body defines a user-written constructor that happens to perform no statements.
Prefer = default when the compiler-generated behavior is exactly what you want.
Phần 6
6. Common Mistakes
6.1. Confusing a Defaulted Function with a Default Argument
Defaulted function:
Order() = default;Default argument:
void print(int count = 1);They are different language features.
6.2. Using = default on an Ordinary Function
This is invalid:
int calculate() = default;= default is intended for special member functions generated by the compiler.
6.3. Expecting Members to Receive Meaningful Values Automatically
class Order {
public:
Order() = default;
private:
int id;
};The defaulted constructor does not automatically set id to a useful value.
Use a default member initializer:
class Order {
public:
Order() = default;
private:
int id{};
};Now id starts at zero.
Phần 7
7. Small Practical Example
#include <iostream>
class Order {
public:
Order() = default;
Order(int orderId, int quantity)
: orderId(orderId), quantity(quantity) {}
void print() const {
std::cout << "Order ID: " << orderId << '\n';
std::cout << "Quantity: " << quantity << '\n';
}
private:
int orderId{};
int quantity{};
};
int main() {
Order emptyOrder;
Order buyOrder(101, 50);
emptyOrder.print();
buyOrder.print();
return 0;
}Order() = default allows this:
Order emptyOrder;The second constructor allows this:
Order buyOrder(101, 50);Phần 8
8. When Should You Use = default?
Use it when:
1. You want the compiler's normal implementation.
2. You want to make that intention explicit in the class interface.
3. Another constructor exists, but you also want a no-argument constructor.
4. You do not need custom logic in the special member function.Write your own function body when custom work is required.
Phần 9
9. End-of-Day Checklist
You should remember:
= default asks the compiler to generate a special member function.You should be able to answer:
1. What does Order() = default mean?
2. Why might it be needed after adding another constructor?
3. Is = default the same as a default argument?
4. Can an ordinary function use = default?
5. Does a defaulted constructor automatically give every int a useful value?Phần 10
10. Conclusion
Defaulted functions express that the compiler-generated behavior is the intended behavior.
The key idea:
Use = default when the compiler's normal implementation is exactly what the class needs.Mã mẫu hoàn chỉnh
cpp11/14_defaulted function/main.cpp
#include <iostream>
class Order {
public:
Order() = default;
Order(int id, int quantity)
: id(id), quantity(quantity) {
}
void print() {
std::cout << "Order ID: " << id << '\n';
std::cout << "Quantity: " << quantity << '\n';
}
private:
int id{};
int quantity{};
};
int main() {
Order emptyOrder;
Order buyOrder(101, 50);
emptyOrder.print();
buyOrder.print();
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