1. 将引用作为返回值的通常原因是避免创建副本以提高效率,包括其他的函数传参亦是如此

  2. std::nothrow可以消除抛出异常

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    #include <iostream>
    #include <new>

    int main()
    {
    try {
    while (true) {
    new int[100000000ul]; // throwing overload
    }
    } catch (const std::bad_alloc& e) {
    std::cout << e.what() << '\n';
    }

    while (true) {
    int* p = new(std::nothrow) int[100000000ul]; // non-throwing overload
    if (p == nullptr) {
    std::cout << "Allocation returned nullptr\n";
    break;
    }
    }
    }

输出:

1
2
std::bad_alloc
Allocation returned nullptr

  1. 枚举用法——类静态成员

    1
    2
    3
    4
    class year {
    private:
    enum {MONTH = 12}; // 可以当做一个 static const
    };

    具体使用场景自己探索哦~

  2. 可以通过terminate()(默认行为)、abort()、exit()来终止程序

  3. 虽然算法、数据结构是基础,但是设计模式也一定要看