├── LICENSE ├── Module1_shared_ptr ├── README.md └── shared_ptr.cpp ├── Module2_unique_ptr ├── README.md └── unique_ptr.cpp ├── Module3_weak_ptr └── README.md ├── Module4_move_semantics └── README.md └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 github.com/CodesBay & www.CodesBay.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Module1_shared_ptr/README.md: -------------------------------------------------------------------------------- 1 | # Module - 1 : Usage of shared_ptr<> 2 | 3 | ### 1.1 shared_ptr<> : *Syntax* 4 | 5 | *The shared_ptr is called as* 6 | 7 | 1. shared_ptr ***name of the smart pointer*** 8 | 2. < > ***Angle brackets which can have built in or user defined type*** 9 | 3. var ***A variable name representing the shared_ptr*** 10 | 11 | *Here is the syntactically valid declaration of shared_ptr* 12 | ``` 13 | // For built in types 14 | shared_ptr sp; 15 | shared_ptr sp1; 16 | shared_ptr sp2; 17 | 18 | // For user defined types 19 | shared_ptr sp; 20 | shared_ptr sp1; 21 | shared_ptr sp2; 22 | 23 | ``` 24 | ### 1.2 shared_ptr : *No need to clear memory explicitly* 25 | 26 | *The main advantage of shared pointers is that, we should not worry about calling delete or cleaning the memory in an explicit manner. The shared pointer will take care of it once it goes out of scope* 27 | 28 | *here is the example of using the shared pointer with built in type* **int** 29 | 30 | *Don't forget to use a header file directive* **#include < memory >** *and using std namespace for using smart pointers* 31 | 32 | ``` 33 | shared_ptr sp (new int); 34 | *sp = 100; // Using the shared_ptr 35 | cout << *sp << endl; 36 | 37 | ``` 38 | *We don't need to free / delete the sp (new int), it will be done automatically when sp goes out of scope.* 39 | 40 | *here is another example of using shared_ptr for user defined types i.e* **class / struct** 41 | 42 | ``` 43 | class Sample { 44 | public: 45 | Sample() { cout << "Sample Constructor" << endl; } 46 | ~Sample() { cout << "Sample Destructor" << endl; } 47 | void publicFn() { cout << "This is public function of class" << endl; } 48 | }; 49 | int main() { 50 | shared_ptr sp(new Sample{}); 51 | sp->publicFn(); 52 | return 0; 53 | } 54 | 55 | ``` 56 | *In this example above, we can see the Destructor is getting called as a result of releasing memory* 57 | 58 | ### 1.3 shared_ptr : *Ownership and Reference Counts* 59 | 60 | *The shared_ptr manages the lifetime of an object based on the Reference counts mechanism.* 61 | 62 | *Which means that multiple shared_ptr can refer to one object. The object in question will NOT be destroyed till any one shared_ptr refers to it* 63 | 64 | *This is akin to Java Garbage Collector which deallocated memory if and only if there is no reference to it.* 65 | 66 | *The example below will demonstrate how this reference count works* 67 | 68 | ``` 69 | class Sample { 70 | public: 71 | Sample() { cout << "Sample Constructor" << endl; } 72 | ~Sample() { cout << "Sample Destructor" << endl; } 73 | }; 74 | 75 | shared_ptr Func() { 76 | cout << "Enter Function" << endl; 77 | shared_ptr sp(new Sample{}); 78 | cout << "Exit Function" << endl; 79 | return sp; 80 | } 81 | 82 | int main() { 83 | cout << "Enter Main" << endl; 84 | shared_ptr retSp = Func(); 85 | cout << "Exit Main" << endl; 86 | return 0; 87 | } 88 | 89 | ``` 90 | *In this example, the shared_ptr is created inside function* **Func()** *and returned. Even though the shared_ptr sp goes out of scope, the object Sample is not deleted as a reference is taken by retSp. Hence the order of execution will look like* 91 | 92 | * 1 - Enter Main 93 | * 2 - Enter Function 94 | * 3 - Sample Constructor 95 | * 4 - Exit Function 96 | * 5 - Exit Main 97 | * 6 - Sample Destructor 98 | 99 | *However, the behavior changes if we don't copy a shared_ptr even if we return it from a function. Look at the code below* 100 | 101 | ``` 102 | shared_ptr Func() { 103 | cout << "Enter Function" << endl; 104 | shared_ptr sp(new Sample{}); 105 | cout << "Exit Function" << endl; 106 | return sp; 107 | } 108 | 109 | int main() { 110 | cout << "Enter Main" << endl; 111 | Func(); 112 | cout << "Exit Main" << endl; 113 | return 0; 114 | } 115 | ``` 116 | *In this example, the object Sample will be destroyed in the Func() itself as the ownership is not taken by another shared_ptr. The out will be* 117 | 118 | * 1 - Enter Main 119 | * 2 - Enter Function 120 | * 3 - Sample Constructor 121 | * 4 - Sample Destructor 122 | * 5 - Exit Function 123 | * 6 - Exit Main 124 | 125 | *Here the shared_ptr<> will destroy the Sample Object as ownership was not taken by the calling **func()** *even if its returned from the function* 126 | 127 | ### 1.4 shared_ptr : *Usage of make_shared<>()* 128 | 129 | *Till now we're using* **new** *to allocate the memory for the objects contained in shared_ptr<>. The standard library has a built in function for it and called as make_shared<>. Here is how we can use it.* 130 | 131 | ``` 132 | class Sample { 133 | public: 134 | Sample() { cout << "Constructor make_shared" << endl; } 135 | ~Sample() { cout << "Destructor make_shared" << endl; } 136 | }; 137 | int main() { 138 | shared_ptr sp = make_shared(); 139 | return 0; 140 | } 141 | 142 | ``` 143 | *The make_shared<> call is not only a clean way to initializing shared_ptr<>, but also fast in memory allocation as compared to new. That's why it's recommended to use make_shared<> instead of new* 144 | 145 | ## 1.5 : Using shared_ptr<> for Arrays 146 | 147 | _The usage of shared_ptr with arrays is slightly complicated as we need to explicitly tell the shared_ptr to call array deleter instead of normal deleted. if we miss to do that then it will result in a memory leak._ 148 | 149 | _For example a shared_ptr with arrays can be created as_ 150 | 151 | ``` 152 | shared_ptr sp(new int[10]); 153 | 154 | ``` 155 | _This will create 10 integers, but will deleter only 1 which will result in memory leaks despite of using shared_ptr_ 156 | 157 | _The code should instead be written as_ 158 | 159 | ``` 160 | shared_ptr sp(new int[10], default_delete()); 161 | 162 | ``` 163 | _The default_delete will make sure that_ `delete[]` _is called to clean up the memory occupied by the array_. 164 | 165 | ***NOTE: Array can't be created using make_shared as there is no way to provide default delete with make_shared 166 | -------------------------------------------------------------------------------- /Module1_shared_ptr/shared_ptr.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT Licence 3 | * Copyright 2017 @CodesBay 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the 6 | * Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, 7 | * and to permit persons to whom the Software is furnished to do so, subject to the following conditions : 8 | 9 | * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 10 | 11 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 12 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 13 | * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH 14 | * THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 15 | */ 16 | 17 | /* 18 | * visit https://github.com/9lean/CplusPlus_SmartPointer for description of the code 19 | */ 20 | 21 | #include 22 | #include 23 | using namespace std; 24 | 25 | // A Sample class with prints in constructor and destructor, so that we can track when the memory 26 | // is allocated and deallocated 27 | 28 | class Sample { 29 | public: 30 | Sample() { cout << "Sample Constructor" << endl; } 31 | ~Sample() { cout << "Sample Destructor" << endl; } 32 | void publicFn() { cout << "This is public function of class" << endl; } 33 | }; 34 | 35 | // Test to confirm that shared pointers do release memory without explicitely calling 36 | // the delete 37 | void TestSharedPtr_ReleaseMemory() { 38 | shared_ptr sp(new Sample{}); 39 | sp->publicFn(); 40 | return; 41 | } 42 | 43 | // A helper function which returns a shared pointer 44 | shared_ptr AFunc() { 45 | cout << "Enter AFunc" << endl; 46 | shared_ptr sp(new Sample{}); 47 | cout << "Exit AFunc" << endl; 48 | return sp; 49 | } 50 | 51 | // Test to confirm that shared pointers does use reference counts 52 | void TestSharedPtr_ReferenceCount() { 53 | cout << "Enter TestSharedPtr_ReferenceCount" << endl; 54 | shared_ptr retSp = AFunc(); 55 | cout << "Exit TestSharedPtr_ReferenceCount" << endl; 56 | return; 57 | } 58 | // Test to confirm that shared pointers does use reference counts and use it smartly 59 | void TestSharedPtr_ReferenceCount_Assignments() { 60 | cout << "Enter TestSharedPtr_ReferenceCount_Assignments" << endl; 61 | AFunc(); 62 | cout << "Exit TestSharedPtr_ReferenceCount_Assignments" << endl; 63 | return; 64 | } 65 | 66 | // Test for using inbuilt Make_Shared to create a shared pointer 67 | void TestSharedPtr_MakeShared() { 68 | shared_ptr sp = make_shared(); 69 | return; 70 | } 71 | 72 | int main() { 73 | TestSharedPtr_ReleaseMemory(); 74 | cout << "------------------------------------------------" << endl; 75 | TestSharedPtr_ReferenceCount(); 76 | cout << "------------------------------------------------" << endl; 77 | TestSharedPtr_ReferenceCount_Assignments(); 78 | cout << "------------------------------------------------" << endl; 79 | TestSharedPtr_MakeShared(); 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /Module2_unique_ptr/README.md: -------------------------------------------------------------------------------- 1 | # Module - 2 : Usage of unique_ptr<> 2 | 3 | ### 2.1 unique_ptr<> : *Syntax* 4 | 5 | *The unique_ptr<> syntax is similar to shared_ptr<> and is consists of* 6 | 7 | 1. shared_ptr ***name of the smart pointer*** 8 | 2. < > ***Angle brackets which can have built in or user defined type*** 9 | 3. var ***A variable name representing the shared_ptr*** 10 | 11 | *All syntactically valid syntax rules of shared_ptr<> will be applicable to unique_ptr<>* 12 | 13 | ### 2.2 unique_ptr : *No need to clear memory explicitly* 14 | 15 | *Like shared_ptr<>, there is no need to call delete or clean the memory in an explicit manner. The unique pointer will take care of it once it goes out of scope* 16 | 17 | *Here is the example of usage with class Sample* 18 | 19 | ``` 20 | class Sample { 21 | public: 22 | Sample() { cout << "Sample Constuctor" << endl; } 23 | ~Sample() { cout << "Sample Destructor" << endl; } 24 | void publicFn() { cout << "This is public function of class" << endl; } 25 | }; 26 | int main() { 27 | unique_ptr sp(new Sample{}); 28 | sp->publicFn(); 29 | return 0; 30 | } 31 | 32 | ``` 33 | *In this example above, we can see the Destructor is getting called as a result of releasing memory* 34 | 35 | ### 2.3 Difference with shared_ptr<> : *Ownership cannot be transferred* 36 | 37 | *shared_ptr<> maintains reference counts where more than one shared_ptr<> can refer to a single object.* 38 | 39 | *This is prevented by unique_ptr<> and the reason behind naming it as unique. In unique_ptr<>, one and only one unique_ptr<> has the Ownership of the Object and manages its lifetime. Look at the code below for an example* 40 | 41 | ``` 42 | int main() { 43 | unique_ptr up1(new int); 44 | // This is not allowed and will give compilation error 45 | unique_ptr up2 = up1; 46 | return 0; 47 | 48 | ``` 49 | 50 | ### 2.4 The Move Semantics : Transferring unique_ptr<> Ownership 51 | 52 | *Its possible to transfer the ownership using std::move(). However, once std::move() is called then moved pointer will no longer hold the ownership. To understand look at the example code below* 53 | 54 | ``` 55 | int main() { 56 | unique_ptr up1(new int); 57 | // After this line, up1 will no longer hold any reference to the 58 | // memory created. Its lifetime is owned by up2 59 | unique_ptr up2 = move(up1); 60 | return 0; 61 | } 62 | 63 | ``` 64 | 65 | ### 2.5 Ownership of unique_ptr<> : Returning from a Function 66 | 67 | *Just like shared_ptr<>, unique_ptr<> can also be returned from a Function. To understand look at the code below which is exactly similar to what we did with shared_ptr<>* 68 | 69 | ``` 70 | class Sample { 71 | public: 72 | Sample() { cout << "Sample Constuctor" << endl; } 73 | ~Sample() { cout << "Sample Destructor" << endl; } 74 | }; 75 | 76 | unique_ptr Func() { 77 | cout << "Enter Function" << endl; 78 | unique_ptr up(new Sample{}); 79 | cout << "Exit Function" << endl; 80 | return up; 81 | } 82 | 83 | int main() { 84 | cout << "Enter Main" << endl; 85 | unique_ptr retup = Func(); 86 | cout << "Exit Main" << endl; 87 | return 0; 88 | } 89 | 90 | ``` 91 | *The above code compiles successfully and the output is* 92 | 93 | * 1 - Enter Main 94 | * 2 - Enter Function 95 | * 3 - Sample Constructor 96 | * 4 - Exit Function 97 | * 5 - Exit Main 98 | * 6 - Sample Destructor 99 | 100 | *Which is similar to what we got with shared_ptr<>* 101 | 102 | * Why? Isn't transfer of ownership is prohibited in unique_ptr<> ? 103 | 104 | *Yes, but as per C++ specification, compilers explicit add std::move() while returning the unique_ptr<> from a function which transfers the ownership* 105 | 106 | *The compilers modified code for the Func() will look like* 107 | 108 | ``` 109 | unique_ptr Func() { 110 | unique_ptr up(new Sample{}); 111 | return move(up); 112 | } 113 | 114 | ``` 115 | ### 2.6 unique_ptr<> : usage of make_unique<>() 116 | 117 | *The* ***C++14*** *specification has added the support of make_unique<>(). Its usage is similar to that of make_shared<>() and should be used instead of* **new** 118 | 119 | ``` 120 | class Sample { 121 | public: 122 | Sample() { cout << "Constructor make_unique" << endl; } 123 | ~Sample() { cout << "Destructor make_unique" << endl; } 124 | }; 125 | int main() { 126 | unique_ptr sp = make_unique(); 127 | return 0; 128 | } 129 | 130 | ``` 131 | ## 2.7 : Using uniqoe_ptr<> for Arrays 132 | 133 | _Unlike shared_ptr<>, unique_ptr<> is quite tolerant to the creation of arrays. It can be done simply by writing the following code with make_unique()_ 134 | 135 | ``` 136 | unique_ptr sp = make_unique(10); 137 | ``` 138 | 139 | * ***Note : All C++14 compilers supports make_unique<>()*** 140 | -------------------------------------------------------------------------------- /Module2_unique_ptr/unique_ptr.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT Licence 3 | * Copyright 2017 @CodesBay 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the 6 | * Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, 7 | * and to permit persons to whom the Software is furnished to do so, subject to the following conditions : 8 | 9 | * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 10 | 11 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 12 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 13 | * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH 14 | * THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 15 | */ 16 | 17 | /* 18 | * visit https://github.com/9lean/CplusPlus_SmartPointer for description of the code 19 | */ 20 | 21 | #include 22 | #include 23 | using namespace std; 24 | 25 | // A Sample class with prints in constructor and destructor, so that we can track when the memory 26 | // is allocated and deallocated 27 | 28 | class Sample { 29 | public: 30 | Sample() { cout << "Sample Constructor" << endl; } 31 | ~Sample() { cout << "Sample Destructor" << endl; } 32 | void publicFn() { cout << "This is public function of class" << endl; } 33 | }; 34 | 35 | // Test to confirm that unique pointers do release memory without explicitely calling 36 | // the delete 37 | void TestUniquePtr_ReleaseMemory() { 38 | unique_ptr up(new Sample{}); 39 | up->publicFn(); 40 | return; 41 | } 42 | 43 | // Test to confirm that unique pointers doesn't transfer the ownership 44 | void TestUniquePtr_NoOwnershipTrasfer() { 45 | unique_ptr up(new Sample); 46 | // The line below will generate compiler error 47 | //unique_ptr upTrans = up; 48 | return; 49 | } 50 | 51 | // Test to confirm that unique pointers ownership can be transferred using std::move 52 | void TestUniquePtr_MoveOwnershipTrasfer() { 53 | unique_ptr up(new Sample); 54 | unique_ptr upTrans = std::move(up); 55 | return; 56 | } 57 | 58 | // A helper function which returns a unique pointer 59 | unique_ptr AFunc() { 60 | cout << "Enter AFunc" << endl; 61 | unique_ptr up(new Sample{}); 62 | cout << "Exit AFunc" << endl; 63 | return up; 64 | } 65 | 66 | // Test to confirm that unique pointers can be returened from a function without 67 | // sharing the ownership 68 | void TestUniquePtr_ReturnFunction() { 69 | cout << "Enter TestUniquePtr_ReturnFunction" << endl; 70 | unique_ptr retSp = AFunc(); 71 | cout << "Exit TestUniquePtr_ReturnFunction" << endl; 72 | return; 73 | } 74 | // Test to confirm that unique pointers does free up the memory if no one is willing to take 75 | // the ownership 76 | void TestUniquePtr_ReturnNoOwnership() { 77 | cout << "Enter TestUniquePtr_ReturnNoOwnership" << endl; 78 | AFunc(); 79 | cout << "Exit TestUniquePtr_ReturnNoOwnership" << endl; 80 | return; 81 | } 82 | 83 | // Test for using inbuilt Make_Unique to create a unique pointer 84 | // Available only in C++14 onwards 85 | void TestUniquePtr_MakeUnique() { 86 | unique_ptr sp = make_unique(); 87 | return; 88 | } 89 | 90 | int main() { 91 | TestUniquePtr_ReleaseMemory(); 92 | cout << "------------------------------------------------" << endl; 93 | TestUniquePtr_MoveOwnershipTrasfer(); 94 | cout << "------------------------------------------------" << endl; 95 | TestUniquePtr_ReturnFunction(); 96 | cout << "------------------------------------------------" << endl; 97 | TestUniquePtr_ReturnNoOwnership(); 98 | cout << "------------------------------------------------" << endl; 99 | TestUniquePtr_MakeUnique(); 100 | return 0; 101 | } 102 | -------------------------------------------------------------------------------- /Module3_weak_ptr/README.md: -------------------------------------------------------------------------------- 1 | # Module - 3 : Usage of weak_ptr<> 2 | 3 | ### 3.1 unique_ptr<> : *Syntax* 4 | 5 | *The weak_ptr<> syntax is similar to shared_ptr<> and is consists of* 6 | 7 | 1. weak_ptr ***name of the smart pointer*** 8 | 2. < > ***Angle brackets which can have built in or user defined type*** 9 | 3. var ***A variable name representing the weak_ptr*** 10 | 11 | *All other syntactically valid syntax rules of shared_ptr<> will be applicable to weak_ptr<> with one exception* 12 | 13 | * weak_ptr<> can't be instantiated 14 | 15 | ### 3.2 weak_ptr : *Why weak_ptr<> is weak?* 16 | 17 | *Unlike shared_ptr<> & unique_ptr<>, weak_ptr<> doesn't take the ownership of the memory contained by it.* 18 | 19 | *Here is the example of usage with class Sample* 20 | 21 | ``` 22 | class Sample { 23 | public: 24 | Sample() { cout << "Cons.." << endl; } 25 | ~Sample() { cout << "Dest.." << endl; } 26 | }; 27 | 28 | int main() { 29 | shared_ptr sp = make_shared(); 30 | cout << sp.use_count() << endl; // Prints 1 31 | weak_ptr wp = sp; 32 | cout << sp.use_count() << endl; // Still Prints 1 33 | return 0; 34 | } 35 | 36 | ``` 37 | *In this example above, we can see that the Object reference count in shared_pointer doesn't incremented* 38 | 39 | ### 3.3 Difference with shared_ptr<> : *Where it can be used?* 40 | 41 | *weak_ptr<> doesn't take the Ownership, so it shall be used only in the places where one doesn't need to worry about maintaining the Ownership.* 42 | 43 | *For example, in the code below, the weak_ptr<> will be invalid once the code comes out of the function* 44 | 45 | ``` 46 | class Sample { 47 | public: 48 | Sample() { cout << "Cons.." << endl; } 49 | ~Sample() { cout << "Dest.." << endl; } 50 | }; 51 | 52 | weak_ptr func() { 53 | shared_ptr Sp = make_shared(); 54 | return Sp; 55 | } 56 | 57 | int main() { 58 | weak_ptr Wp = func(); 59 | cout << "End of Main" << endl; 60 | return 0; 61 | } 62 | 63 | ``` 64 | 65 | *In the example above, "Wp" will be invalid in main() as it doesn't take the ownership and was destroyed by shared_ptr<> before coming out of the func() scope* 66 | -------------------------------------------------------------------------------- /Module4_move_semantics/README.md: -------------------------------------------------------------------------------- 1 | # Module - 4 : Usage of std::move with Smart pointers 2 | 3 | ### 4.1 *Usage* 4 | 5 | *As demonstrated in the example of unique_ptr<>, the move is used to transfer the ownership and can be used by both unique_ptr<> as well as by shared_ptr<>* 6 | 7 | ### 4.2 std::move : *usage with unique_ptr<>* 8 | 9 | *In unique_ptr<>, move is used to transfer the ownership from one unique_ptr<> to another, which is otherwise not possible* 10 | 11 | ``` 12 | int main() { 13 | unique_ptr up (new int); 14 | // transfer the ownership 15 | unique_ptr up2 = move(up); 16 | // After this like, up doesn't holds the ownership of the 17 | // object create by new int 18 | return 0; 19 | } 20 | 21 | ``` 22 | 23 | ### 4.3 std::move : *usage with unique_ptr<>* 24 | 25 | *shared_ptr<>, as the name suggests, multiple pointers can share the ownership with the help of reference counts*. 26 | 27 | *However, std::move behaves in the similar way as with unique_ptr<>. It will transfer the ownership (and not increment the reference count) of the shared_ptr* 28 | 29 | ``` 30 | int main() { 31 | shared_ptr sp (new int); 32 | cout< sp2 = move(sp); 35 | // After this like, up doesn't holds the ownership of the 36 | // object create by new int 37 | cout< 10 | * unique_ptr<> 11 | * weak_ptr<> 12 | 13 | *They are pretty much sufficient to handle most of the programming needs related to dynamically allocated memory.* 14 | 15 | *The modules below (1,2,&3) are dedicated to respective pointers. Please do not escape the sequence as module 2 refers to module 1 and module 3 refers to module 1 & 2.* 16 | 17 | [Module - 1 : Usage of shared_ptr<>](https://github.com/9lean/CplusPlus_SmartPointer/tree/master/Module1_shared_ptr) 18 | 19 | *This module will describe the properties of shared_ptr<> as well as how the use this smart pointer in C++ programs* 20 | 21 | [Module - 2 : Usage of unique_ptr<>](https://github.com/9lean/CplusPlus_SmartPointer/tree/master/Module2_unique_ptr) 22 | 23 | *This module will describe the properties of unique_ptr<> as well as how the use this smart pointer in C++ programs* 24 | 25 | *This program will use examples of module 1 to properly demonstrate the difference between these two pointers.* 26 | 27 | [Module - 3 : Using of weak_ptr<>](https://github.com/9lean/CplusPlus_SmartPointer/tree/master/Module3_weak_ptr) 28 | 29 | *This module will describe the properties of weak_ptr<> as well as how the use this smart pointer in C++ programs* 30 | 31 | *The weak_ptr<> is generally used with shared_ptr<> and this module will use both of them drastically* 32 | 33 | [Module - 4 : The std::move semantics](https://github.com/9lean/CplusPlus_SmartPointer/tree/master/Module4_move_semantics) 34 | 35 | *This module will describe the move semantics and how std::move can be used with shared_ptr<> and unique_ptr<>.* 36 | 37 | *This will also touch upon when and how to use std::move()* 38 | --------------------------------------------------------------------------------