Website Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commit1aa1048deef6f53bef188b593f67f28e79171b97 (patch)
Author
 Angel Dimitriev <achi_d@abv.bg>
2020-03-15 22:13:26 +0200
Committer
 GitHub <noreply@github.com>
2020-03-15 22:13:26 +0200
Filesa0ce1a440d40ca05c7f47b052346acc35bd0747e
Object-oriented_programming_FMI-1aa1048deef6f53bef188b593f67f28e79171b97.tar
Object-oriented_programming_FMI-1aa1048deef6f53bef188b593f67f28e79171b97.tar.gz
Object-oriented_programming_FMI-1aa1048deef6f53bef188b593f67f28e79171b97.zip
Parent91d8b570ea0488986463fb3d14cc3ed5aeb3c216 (diff)
Create 3. weak_ptr.cpp
1 files changed, 60 insertions, 0 deletions
-rw-r--r--Smart_pointers/3. weak_ptr.cpp60
diff --git a/Smart_pointers/3. weak_ptr.cpp b/Smart_pointers/3. weak_ptr.cpp
new file mode 100644
index 0000000..266ae3a
--- /dev/null
+++ b/Smart_pointers/3. weak_ptr.cpp
@@ -0,0 +1,60 @@
+/******************************************************************************
+
+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 <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();
+
+ cout << smart_pointer_1.use_count() << endl;//2
+
+ smart_pointer_3->f(); // .. и тн.
+
+}