ltimaginea
ltimaginea
## **Should add the discussion about `shared_ptr&&` and `unique_ptr&&` in [Smart pointer rule summary](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rr-summary-smartptrs) .** Link: [Smart pointer rule summary](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rr-summary-smartptrs) For [R.34](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rr-sharedptrparam-owner)'s example code, when enter the function, the shared_ptr...
C.65 Link: [C.65: Make move assignment safe for self-assignment](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rc-move-self)  If someone move-assigns an object to itself, line `delete ptr;` deletes both `this->ptr` and `other.ptr` (i.e., `temp`) since `*this` and...
https://github.com/parallel101/course/blob/c8787cf47d60ec7a72ce6ccfc9ba6202e890c8bd/02/19/main.cpp#L32-L35 课件这里的写法是复杂又容易出错的,其实我们可以采取下面这样更安全的方式: ```cpp //C* raw_p = p.get(); // no need func(std::make_unique(*p)); // deep copy p->do_something(); // OK, run normally ``` 虽然 `std::unique_ptr` 删除了 copy constructor 和 copy assignment operator ,但其实我们可以借助解引用操作变通地对...
# Issue1 https://github.com/ShiqiYu/CPP/blob/14c70552862fffb0463e596607ff6f95dcd6d804/week12/examples/derived-memory/mystring.hpp#L68 派生访问说明符拼写错了,应该是 `public` 而不是 `pubic` 。 # Issue2 https://github.com/ShiqiYu/CPP/blob/14c70552862fffb0463e596607ff6f95dcd6d804/week12/examples/derived-memory/mystring.hpp#L76-L80 1. 派生类的构造函数、拷贝构造函数和拷贝赋值运算符通常应该是 `public` 而不需要是 `private` 。 2. 派生类的拷贝构造函数的规范实现应该是调用基类的拷贝构造函数来进行初始化派生类对象的基类部分,课件这里,调用基类的普通构造函数是不合适的,并且程序也是无法通过编译的,因为基类MyString的数据成员是 `private` 的,所以派生类MyMap是无法直接访问基类的 `buf_len` 和 `characters` 数据成员的。 派生类MyMap的拷贝构造函数的规范实现代码如下: ```cpp public: MyMap(const MyMap&...
https://github.com/ShiqiYu/CPP/blob/14c70552862fffb0463e596607ff6f95dcd6d804/week12/examples/virtual.cpp#L5-L14 The class Person doesn't have a user-written virtual destructor, so its destructor defaults to public non-virtual. https://github.com/ShiqiYu/CPP/blob/14c70552862fffb0463e596607ff6f95dcd6d804/week12/examples/virtual.cpp#L49-L51 The result of `delete p;` is undefined behavior, so it might call...
https://github.com/ShiqiYu/CPP/blob/8d450448b8006f82fed677deb0d3639bf2226915/week11/examples/example2/mystring.hpp#L29-L48 # Issue1: can't handle self-assignment See: [Assignment Operators, C++ FAQ (isocpp.org)](https://isocpp.org/wiki/faq/assignment-operators) If `x = x` , bad errors will occur. We can handle self-assignment by explicitly testing for self-assignment:...
https://github.com/ShiqiYu/CPP/blob/8d450448b8006f82fed677deb0d3639bf2226915/week11/examples/unique_ptr.cpp#L56 课件这里,其实可以这样进行拷贝: `std::unique_ptr mt3 = std::make_unique(*mt1);` 虽然 `std::unique_ptr` 删除了 copy constructor 和 copy assignment operator ,但其实我们可以借助解引用操作变通地对 `std::unique_ptr` 进行拷贝。 deep copy 示例如下: ```cpp std::unique_ptr up1(std::make_unique("Good morning")); // copy construct! std::unique_ptr up2(std::make_unique(*up1));...
In this DesignPatterns repository: 1. All example codes forget to explicitly define a `virtual` destructor in polymorphic base classes. 2. All example codes forget to `delete` memory previously allocated by...