CS106L - 24 winter#
这里是CS106L的课程资料,包括课程大纲、课程作业、课程讲义等。
Slide Note#
lec3 Initialization & referrence#
- 初始化
- referrence
- cv-type
- You can’t declare a non-const reference to a const variable
lec4 Stream#
-
cerr and clog
cerr: used to output errors clog: used for non-critical event logging
-
getline
注意getline函数的使用,最好不要和cin混用,否则会出现问题。
lec5 Class#
- How to write .h and .cpp file
- To make alias
- How to use
this
pointer -
Container adapters:例如queue中的deque和list
-
virtual 关键词 用于声明虚函数。虚函数是在基类中使用 virtual 关键字声明的成员函数,它可以在派生类中被重写(覆盖)。:
而如果使用纯虚函数,那么每个基类使用都一定要重写这个函数,否则会报错。
一段示例代码:
请注意constructor变量名的命名技巧,这样可以避免和成员变量重名。
lec6 Template class#
-
为什么要使用模板类? - 代码复用 - 类型安全 - 性能优化
-
在.h文件中必须包含.h和.cpp文件
-
const 的使用
由于编译器并不知道std::string stringify(const Student& s){ return s.getName() + " is " + std::to_string(s.getAge()) + " years old." ; }
getName()
&getAge()
函数是否会改变s的值,所以我们需要在函数后面加上const关键词。 -
throw exception
-
const_cast 使用技巧
lec7 template function#
- An example
- Constraints and concepts
Complex Reference
3. 使用template可以更有效率:
template<unsigned1>
struct Factorial {
enum value =n * Factorial<n - 1>::value };
};
template<>// template class "specializationstruct
Factorial<0>{enum fvalue=1};
std::cout <<Factorial<10>::value << endl;//prints 3628800, but run duringcompile time!
lec8 function and lambda#
- function pointer
- lambda expression
- Functor
- virtual function: can be overrided by derived class
- The STL implements tons of cool algorithms that we can use without rewriting them!
lec9 Operator Overloading#
-
fraction example: https://replit.com/@havenw/simplefraction
-
friend关键词可以让一个函数访问类的私有成员,它有助于实现重载运算符,但请注意他会破坏封装性。
lec10 Special Member Function#
Class has three main part: the constructor and destructor, member variables, and functions.
Totaly, there are 6 special member functions: - Default constructor - Copy constructor - Copy assignment operator - Move constructor - Move assignment operator - Destructor
The compiler will generate these functions for you if you don't define them yourself. However, if you define any of these functions, the compiler will not generate the others for you.
Example:
- 浅拷贝和深拷贝
#include <iostream> class ShallowCopyExample { private: int* data; public: // 浅拷贝的构造函数 ShallowCopyExample(const ShallowCopyExample& other) { data = other.data; // 只是复制指针,而不是复制指针指向的数据 } // 深拷贝的构造函数 DeepCopyExample(const DeepCopyExample& other) { data = new int(*other.data); // 分配新的内存,并复制数据 } // 析构函数 ~ShallowCopyExample() { delete data; // 释放动态分配的内存 } }; int main() { ShallowCopyExample original; ShallowCopyExample shallowCopy = original; // 浅拷贝 DeepCopyExample deepCopy = original; // 深拷贝 return 0; }
attention
If you have to define a destructor, copy constructor, or copy assignment operator, you should define all three! - Needing one signifies you’re handling certain resources manually. - We then should handle the creation, assignment, use, and destruction of those resources ourselves!
lec RAII & smart pointer#
- 一段代码中存在许多种可能的code path, 我们需要留意是否会造成内存泄漏。 | | Acquire | Release| |Heap Memory | new | delete| | Files | open | close| | Locks | try_lock | unlock| | Socket | socket | close |
-
Exception:
-
Unique_pointer: can not be copied.
Created: 2023年7月4日 20:38:52