Weak Pointer in C++In this article, we will discuss the weak pointer in C++ with their syntax and examples. The C++ weak_ptr is a standard library component. It is used to store weak references to any objects controlled by the shared_ptr standard library pointer, which is used to ultimately transform weak_ptr to shared_ptr. The shared_ptr that was ultimately created from the weak_ptr is used to access the referenced object. A weak_ptr loses its capacity to remain forever after it is transformed into a shared_ptr, indicating that it only exists momentarily. It is not advised to access weak_ptr first before granting access to the reference object. Syntax:It has the following syntax: The type controlled by the weak pointer is used for the parameter supplied as Class T according to the syntax for the C++ weak_ptr function. How does C++'s weak_ptr operate?Every pointer in a high-level language is primarily used to reference objects and subsequently access elements that are available in an array in a well-organized style. Similar circumstances apply to C++'s weak_ptr. The weak_ptr working flow is described throughout the class template:
Examples of weak_ptr in C++:Here are some examples of weak_ptr in C++: Example - 1: Output: Weak pointer value: 42 Weak pointer is expired after reset Explanation: In this example, the shared pointer shared_Ptr points to an int with the value 42. After that, a sharedPtr and a std::weak_ptrweakPtr are created. If the original object has been removed, the Lock() function returns an empty shared pointer, which is used to retrieve a shared pointer from a weak pointer. The program prints the shared object's value first when it uses the weak pointer to access it. After that, the shared pointer is reset, simulating a scenario in which the initial shared pointer no longer controls the object. After resetting the shared pointer, the program tries to access the object again using the weak pointer. Example - 2: Output: Acquired weak_ptr through Lock(): 14 Weak pointer expired: false Weak pointer expired after reset: true Example - 3: Output: Count of sharedPtr1: 1 Count of weakPtr: 1 Count of sharedPtr1 after sharedPtr2 creation: 2 Count of weakPtr after sharedPtr2 creation: 2 Example - 4: Output: *shrd_ptr_1 == 8 *shrd_ptr_1 == 10 *shrd_ptr_1 == 8 *wk_ptr_1 == 8 *wk_ptr_2 == 10 *wk_ptr_1 == 8 Explanation: This program shows how to utilize the swap() method to swap the weak_ptr when it's necessary to get the needed resource, as seen by the output. In the shared_ptr instances, the shrd_ptr_1 and shrd_ptr_2 that point to the integers 8 and 10 are created at the beginning of the supplied program. After that, the value of shrd_ptr_1 (which is 8) is printed. Next, it uses both the member function swap and the swap function from the standard library to swap the contents of shrd_ptr_1 and shrd_ptr_2. As a result, shrd_ptr_1's value shifts from 8 to 10 and back again. As part of the shared_ptr instances, the program creates two weak_ptr instances, wk_ptr_1 and wk_ptr_2. wk_ptr_1's value, 8, is printed by t. After that, the ownership of wk_ptr_1 and wk_ptr_2 are switched using the member function swap and the swap function from the standard library. After the values of wk_ptr_1 and wk_ptr_2 are switched, wk_ptr_1 now contains the number 10. The program ends by switching wk_ptr_1 and wk_ptr_2 once more to return to the previous ownership. After that, the value of wk_ptr_1 is printed, which returns 8. Conclusion:The C++ weak_ptr function is essential for obtaining and accessing the list node's elements. Additionally, shared_ptr and weak_ptr work together to create an optimized cycle for accessing the elements. Once shared_ptr has been chosen, it is generally thought of as a permanent operation. The C++ weak_ptr function offers numerous advantages for resource acquisition. Next TopicApriori Algorithm Implementation in C++ |