From 2d09c8b018054743dcd098be08afb03e5d1ddd8d Mon Sep 17 00:00:00 2001 From: Angel Dimitriev Date: Sun, 15 Mar 2020 21:44:39 +0200 Subject: Update and rename 1. uniqe_ptr to 1. uniqe_ptr.cpp --- Smart_pointers/1. uniqe_ptr.cpp | 58 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Smart_pointers/1. uniqe_ptr.cpp (limited to 'Smart_pointers/1. uniqe_ptr.cpp') diff --git a/Smart_pointers/1. uniqe_ptr.cpp b/Smart_pointers/1. uniqe_ptr.cpp new file mode 100644 index 0000000..c5637a4 --- /dev/null +++ b/Smart_pointers/1. uniqe_ptr.cpp @@ -0,0 +1,58 @@ +/****************************************************************************** + + Online C++ Compiler. + Code, Compile, Run and Debug C++ program online. +Write your code in this editor and press "Run" button to compile and execute it. + +*******************************************************************************/ + +#include +#include + +using namespace std; + +class Test +{ +public: + Test() + { + cout<<"Test()"< smart_pointer_1(new Test()); //Test() + + Test* obj = smart_pointer_1.get(); //От тук иматаме достъп до паметта, към която сочи пойнтера. + //delete obj; // Това не трябва да се прави! smart_pointer_1 не знае, че паметта е освободена и ще се опита да я освободи, което ще доведе до грешка! + + } //Test (Паметта се освобождава сама) + + { + + unique_ptr smart_pointer_2(new Test()); + + unique_ptr smart_pointer_3 = move(smart_pointer_2); + // unique_ptr smart_pointer_3(smart_pointer_2); //този синтаксис (както и оператор =) не е валиден! Те бяха валидни в стария и не толкова иползван auto_ptr. + + smart_pointer_2.get(); // Това ще даде NULL, защото smart_pointer_3 "открадна" обекта на smart_pointer_2 + + } + + + return 0; +} -- cgit v1.3.1-23-gd82d