From 6d495944d4ab5ed032d9f4cbadbb963887327737 Mon Sep 17 00:00:00 2001 From: Angel Dimitriev Date: Sun, 15 Mar 2020 21:56:00 +0200 Subject: Update 2. shared_ptr.cpp --- Smart_pointers/2. shared_ptr.cpp | 84 +++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 45 deletions(-) diff --git a/Smart_pointers/2. shared_ptr.cpp b/Smart_pointers/2. shared_ptr.cpp index fc86905..3688134 100644 --- a/Smart_pointers/2. shared_ptr.cpp +++ b/Smart_pointers/2. shared_ptr.cpp @@ -1,10 +1,4 @@ -/****************************************************************************** - 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 @@ -14,47 +8,47 @@ using namespace std; class Test { public: - Test() - { - cout<<"Test()"< smart_pointer_1(new Test()); - - cout<< smart_pointer_1.get() < 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 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; + + shared_ptr smart_pointer_1(new Test()); + + cout << smart_pointer_1.get() << endl; //адресът на обекта. + + shared_ptr 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 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 и обекта се изтрива(изтрива се от последния пойнтер преди да изчезне) -- cgit v1.3.1-23-gd82d