Website Summary Files Commits Branches @commit @diff @stats
path: root / Smart_pointers / 1. uniqe_ptr.cpp
diff options
context:
space:
mode:
Commit0218c678e13e1cb94a352c91733cea68467dc566 (patch)
Author
 Angeld55 <angel_dimitriev@abv.bg>
2021-02-14 23:06:35 +0200
Committer
 Angeld55 <angel_dimitriev@abv.bg>
2021-02-14 23:06:35 +0200
Files16fb3114cb560242458d10411765e50f5d6183db /Smart_pointers/1. uniqe_ptr.cpp
Object-oriented_programming_FMI-0218c678e13e1cb94a352c91733cea68467dc566.tar
Object-oriented_programming_FMI-0218c678e13e1cb94a352c91733cea68467dc566.tar.gz
Object-oriented_programming_FMI-0218c678e13e1cb94a352c91733cea68467dc566.zip
Parentd4ae0ef9ec1b54420176c4a88e09f2fd44ae31b0 (diff)
start from scratch
1 files changed, 0 insertions, 56 deletions
-rw-r--r--Smart_pointers/1. uniqe_ptr.cpp56
diff --git a/Smart_pointers/1. uniqe_ptr.cpp b/Smart_pointers/1. uniqe_ptr.cpp
deleted file mode 100644
index a865776..0000000
--- a/Smart_pointers/1. uniqe_ptr.cpp
+++ /dev/null
@@ -1,56 +0,0 @@
-#include <iostream>
-#include <memory>
-
-using namespace std;
-
-class Test
-{
-public:
- Test()
- {
- cout<<"Test()"<<endl;
- }
- Test(const Test& other)
- {
- cout<<"Copy constructor"<<endl;
- }
- Test& operator=(const Test& other)
- {
- cout<< "Assignment operator" <<endl;
- }
- ~Test()
- {
- cout<<"Detructor"<<endl;
- }
- void f()
- {
-
- }
-
-};
-int main()
-{
- {
- unique_ptr<Test> smart_pointer_1(new Test()); //Test()
-
- smart_pointer_1->f(); // Достъпваме данните, както с обикновен пойнтер.
-
- Test* obj = smart_pointer_1.get(); //От тук иматаме достъп до паметта, към която сочи пойнтера.
- //delete obj; // Това не трябва да се прави! smart_pointer_1 не знае, че паметта е освободена и ще се опита да я освободи, което ще доведе до грешка!
-
- } // Detructor (Паметта се освобождава сама след премахването на пойнтера)
-
- {
-
- unique_ptr<Test> smart_pointer_2(new Test());
-
- unique_ptr<Test> smart_pointer_3 = move(smart_pointer_2);
- // unique_ptr<Test> smart_pointer_3(smart_pointer_2); //този синтаксис (както и оператор =) не е валиден! Те бяха валидни в стария и не толкова иползван auto_ptr.
-
- smart_pointer_2.get(); // Това ще даде NULL, защото smart_pointer_3 "открадна" обекта на smart_pointer_2
-
- }
-
-
- return 0;
-}