Skip to content

Instantly share code, notes, and snippets.

@palindrom615
Last active May 25, 2018 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save palindrom615/1df6c13e32cadc376d3dc28e81f9fa66 to your computer and use it in GitHub Desktop.
Save palindrom615/1df6c13e32cadc376d3dc28e81f9fa66 to your computer and use it in GitHub Desktop.
unique_ptr의 문제점 번역
// C++
int main() {
// std::unique_ptr<string> hoge(new string("hoge")); 와 같습니다.
// 타입을 두번 쓰는 것을 피하고, new 키워드를 없앨 수가 있습니다.
auto hoge = std::make_unique<std::string>("hoge");
auto piyo = std::make_unique<std::string>("piyo");
println(*hoge); // => hoge
println(*piyo); // => piyo
hoge = std::move(piyo); // 변수 piyo에 있는 포인터의 소유권은 변수 hoge로 이동.
println(*hoge); // => piyo
println(*piyo); // 변수 piyo는 초기화되었기 때문에 메모리 접근 위반이 발생합니다.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment