From 0218c678e13e1cb94a352c91733cea68467dc566 Mon Sep 17 00:00:00 2001 From: Angeld55 Date: Sun, 14 Feb 2021 23:06:35 +0200 Subject: start from scratch --- Smart_pointers/3. weak_ptr.cpp | 52 ------------------------------------------ 1 file changed, 52 deletions(-) delete mode 100644 Smart_pointers/3. weak_ptr.cpp (limited to 'Smart_pointers/3. weak_ptr.cpp') 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 -#include - -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 smart_pointer_1(new Test()); - - cout << smart_pointer_1.get() << endl; //адресът на обекта. - - weak_ptr smart_pointer_2(smart_pointer_1); - - cout << smart_pointer_2.use_count() << endl; // 1 - weak_ptr НЕ участва в бройката на пойнтерите към обектите. - - //smart_pointer_2->f(); ГРЕШКА! - //Това е само за временно пазане на адреса! НЕ МОЖЕМ да достъпим данните. Ако искате да достъпите данните на този пойнтер трябва да го превърнете(заключте) в shared_ptr!. - - shared_ptr smart_pointer_3 = smart_pointer_2.lock(); //.lock() връща shared_ptr към обекта. - - cout << smart_pointer_1.use_count() << endl;//2 - - smart_pointer_3->f(); // .. и тн. - -} -- cgit v1.3.1-23-gd82d