In C ++ 11, we can use shared_ptr to manage the ownership of an object and analyze the object structure. However, in some cases, we only want to securely access an object and do not want to have ownership of this object, responsible for the analysis structure (a bit like the irresponsible men in the TV series, it's just fun, it's not responsible ). In this case, we can use weak_ptr that indicates weak references.
Weak_ptr can be built by a shared_ptr, indicating that weak_ptr has the access to the object pointed to by this shared_ptr. Note that this is only the access permission, and it will not change the reference count of the smart pointer, naturally, this object will not be parsed. With weak_ptr, We can securely access those objects without ownership.
An example in reality is the school's transfer room. The transfer room has a list of students. If a phone call comes to find a student, the transfer room will try to access the student based on the roster, if the student is still in school, he will call the student directly. If the student has left, the student will receive a message. Here, the students on the Roster may still be in the school (the object still exists), or may have left the school (the object has been analyzed), and we all need to access it, weak_ptr is used to access such objects that are not sure whether they exist.
# Include <iostream>
# Include <map>
# Include <algorithm>
# Include <memory>
Using namespace std;
// School Students
Class human
{
Public:
Human (string _ n): name (_ n)
{};
~ Human ()
{
Cout <name <"was destructed." <endl;
}
Void call () // Hey, you have a call
{
Cout <name <"was called." <endl;
}
Private:
String name;
};
// Transfer room
Class doorman
{
Public:
Doorman (map <string, shared_ptr {
// Construct a roster based on the student object. Note that weak_ptr constructed by shared_ptr is saved.
For_each (humans. begin (), humans. end (),
[& Names] (pair <string, shared_ptr {
Names [h. first] = weak_ptr });
}
// A call is sent to the Transfer Room.
Void call (string name)
{
// Check whether this student exists in the roster.
Auto it = names. find (name );
// If any
If (it! = Names. end ())
{
Auto man = (* it). second;
// Use the lock () function to obtain the shared_ptr pointed to by weak_ptr and save it as p
If (auto p = man. lock ())
P-> call (); // If the associated shared_ptr is found, that is, the object still exists, that is, the student is still in school.
Else // If the associated shared_ptr cannot be obtained, it indicates that the object does not exist. The student leaves the school and can only leave a message for him.
{
Leavemsg (name );
}
}
Else // if there is no such name in the roster
{
Cout <name <"is not in the school." <endl;
}
}
Void leavemsg (string name)
{
Cout <name <"has left school. I will leave a message for him." <endl;
}
Private:
Map <string, weak_ptr };
Int main ()
{
// Students of the school
Map <string, shared_ptr Humans ["Jiawei"] = make_shared Humans ["Chen"] = make_shared Humans ["Xibei"] = make_shared
// Create a roster based on the students
Doorman dm (humans );
// Someone looks for Chen
Dm. call ("Chen ");
// Someone looks for Fu
Dm. call ("Fu ");
// Chen leaves the school and the object is analyzed
Humans. erase ("Chen ");
// Another user calls Chen. At this time, he is no longer in school and can only leave a message for him.
Dm. call ("Chen ");
// Someone looks for Jiawei. She is still at school and calls her directly.
Dm. call ("Jiawei ");
Return 0;
}
From the output of this program, we can also see that we are deleting the Chen element in the humans container, and the corresponding human object is also analyzed, the weak_ptr pointing to this object in doorman does not affect its structure. When we attempt to access this object again, lock () cannot successfully obtain the shared_ptr associated with it, in this case, you cannot access it.
Chen was called.
Fu is not in the school.
Chen was destructed.
Chen has left school. I will leave a message for him.
Jiawei was called.
Xibei was destructed.
Jiawei was destructed.
Here you may ask, why not use a bare pointer in doorman ?...
So why not use shared_ptr directly? Refer to the original article.
In summary, weak_ptr is used to access objects that do not have ownership and may or may not exist.
Author: "My first C ++ book"