Weak Pointer in C++28 Aug 2024 | 6 min read 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++ |
strcoll() in C++
Before diving into 'strcoll()' in C++, it's essential to understand the broader context of string comparison and the challenges it poses due to different character encodings and locale-specific rules. Let's explore these concepts and then delve into 'strcoll()' specifics. String Comparison in C++: In C++, strings are typically...
6 min read
Make_shared in C++
Writing effective and reliable code in C++ requires careful consideration of memory management issues. One of the most helpful tools for memory management provided by the standard library is the make_shared function. In this blog post, we will examine the make_shared function, its syntax, and how...
3 min read
Cin.get() in C++
In this article, we will discuss the cin.get() function in C++ with its methods and examples. Introduction: The character array can be accessed using the cin.get() function. In the C++ programming language, this fundamental function is used to solicit user feedback. The white space characters are also incorporated...
5 min read
Add two Array in C++
Arrays are an important data structure in C++ as they allow for the storage and manipulation of multiple values in a single variable. They are used to store a collection of elements, all of which have the same data type and are stored in contiguous memory...
4 min read
School Fee Enquiry System in C++
The project's code is written in the C++ programming language. Speaking about the system, the user may explicitly examine a student's fee slip for a class, change the school's fee schedule, and also view the school's fee schedule as a list. The following features are available...
48 min read
Prim's Algorithm in C++
Prim's algorithm is a greedy algorithm used for finding the minimum spanning tree (MST) of a connected, undirected graph. The minimum spanning tree of a graph is a subset of the edges that forms a tree and connects all the vertices in the graph while minimizing...
26 min read
DFA-based division in C++
In this article, you will learn the with its example. Using a Deterministic Finite Automaton (DFA) to Check Divisibility Division using deterministic finite automata (DFA) is a technique that can efficiently implement integer division in hardware. The basic idea is constructing a DFA that recognizes strings representing...
4 min read
Heap in C++ STL
C++ Heap In C++, a heap is a tree-based data structure that is mainly used for priority queues and efficient retrieval of the maximum or minimum element. The C++ STL offers several built-in functions for heap operations, such as make_heap(), push_heap(), pop_heap(), sort_heap(), is_heap, and is_heap_until(). The...
12 min read
Isdigit() function in C++
Data manipulation and validation are made easy with the help of the robust programming language C++. Isdigit() is one such method, and it's very helpful when working with character data. In this article, we will discuss the isdigit() function in detail. We will examine its syntax...
6 min read
How to Square a Number in C++
Multiplying an integer by itself yields the simple mathematical operation known as squaring. It can be accomplished using a simple C++ program. Understanding Squares: An essential mathematics procedure is to square a number. In mathematical notation, squaring a number 'x' is written as 'x^2', where 'x' is the...
3 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India