diff options
start from scratch
3 files changed, 0 insertions, 161 deletions
| -rw-r--r-- | Smart_pointers/1. uniqe_ptr.cpp | 56 | ||||
| -rw-r--r-- | Smart_pointers/2. shared_ptr.cpp | 53 | ||||
| -rw-r--r-- | Smart_pointers/3. weak_ptr.cpp | 52 |
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; -} diff --git a/Smart_pointers/2. shared_ptr.cpp b/Smart_pointers/2. shared_ptr.cpp deleted file mode 100644 index b8437a8..0000000 --- a/Smart_pointers/2. shared_ptr.cpp +++ /dev/null @@ -1,53 +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; - } - -}; -int main() -{ - - - shared_ptr<Test> smart_pointer_1(new Test()); - - cout << smart_pointer_1.get() << endl; //адресът на обекта. - - shared_ptr<Test> smart_pointer_2(smart_pointer_1); - - cout << smart_pointer_1.get() << endl; //За разлика от uniqe_ptr, тук smart_pointer_1 ПРОДЪЛЖАВА да сочи към обекта. - - cout << smart_pointer_2.use_count() << endl; //може да видим колко пойнтера са насочени към обекта. - // Обектът ще се изтрие, когато пойнтерите станат 0. - - - { - shared_ptr<Test> smart_pointer_3 = smart_pointer_2; - - cout << smart_pointer_1.use_count() << endl; //3 пойнтера към обекта ни. - - } - cout << smart_pointer_1.use_count() << endl; // 2, защото smart_pointer_3 вече го няма. - - return 0; -} //тук изчезват пойнтерите -> броят им става 0 и обекта се изтрива(изтрива се от последния пойнтер преди да изчезне) -//Възможно е да се получи цикличност. Един shared_ptr да реферира друг shared_ptr, който обратно да реферира първия! Как да се справим с проблема ще видим в точка 3. (weak_ptr) diff --git a/Smart_pointers/3. weak_ptr.cpp b/Smart_pointers/3. weak_ptr.cpp deleted file mode 100644 index 044985a..0000000 --- a/Smart_pointers/3. weak_ptr.cpp +++ /dev/null @@ -1,52 +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() -{ - - - shared_ptr<Test> smart_pointer_1(new Test()); - - cout << smart_pointer_1.get() << endl; //адресът на обекта. - - weak_ptr<Test> smart_pointer_2(smart_pointer_1); - - cout << smart_pointer_2.use_count() << endl; // 1 - weak_ptr НЕ участва в бройката на пойнтерите към обектите. - - //smart_pointer_2->f(); ГРЕШКА! - //Това е само за временно пазане на адреса! НЕ МОЖЕМ да достъпим данните. Ако искате да достъпите данните на този пойнтер трябва да го превърнете(заключте) в shared_ptr!. - - shared_ptr<Test> smart_pointer_3 = smart_pointer_2.lock(); //.lock() връща shared_ptr към обекта. - - cout << smart_pointer_1.use_count() << endl;//2 - - smart_pointer_3->f(); // .. и тн. - -} |
