We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
course/02/19/main.cpp
Lines 32 to 35 in c8787cf
课件这里的写法是复杂又容易出错的,其实我们可以采取下面这样更安全的方式:
//C* raw_p = p.get(); // no need func(std::make_unique<C>(*p)); // deep copy p->do_something(); // OK, run normally
虽然 std::unique_ptr 删除了 copy constructor 和 copy assignment operator ,但其实我们可以借助解引用操作变通地对 std::unique_ptr 进行拷贝。
std::unique_ptr
deep copy 示例如下:
std::unique_ptr<std::string> up1(std::make_unique<std::string>("Good morning")); // copy construct! std::unique_ptr<std::string> up2(std::make_unique<std::string>(*up1)); // safe copy construct! std::unique_ptr<std::string> up3(up1 ? std::make_unique<std::string>(*up1) : nullptr); // copy assignment! up2 = std::make_unique<std::string>(*up1); // safe copy assignment! up3 = up1 ? std::make_unique<std::string>(*up1) : nullptr;
其它的例证:
The text was updated successfully, but these errors were encountered:
No branches or pull requests
course/02/19/main.cpp
Lines 32 to 35 in c8787cf
课件这里的写法是复杂又容易出错的,其实我们可以采取下面这样更安全的方式:
虽然
std::unique_ptr
删除了 copy constructor 和 copy assignment operator ,但其实我们可以借助解引用操作变通地对std::unique_ptr
进行拷贝。deep copy 示例如下:
其它的例证:
The text was updated successfully, but these errors were encountered: