adding the unique function line

This commit is contained in:
Jidong Xiao
2025-04-18 03:20:44 -04:00
committed by JamesFlare1212
parent 45124db960
commit 284bd60029
2 changed files with 10 additions and 2 deletions

View File

@@ -344,7 +344,7 @@ int main() {
## 27.15 Exercise ## 27.15 Exercise
In the following program, the use count will be printed 3 times. What exact value will be printed each time? In the following [program](shared_ptr1.cpp), the use count will be printed 3 times. What exact value will be printed each time?
```cpp ```cpp
#include <iostream> #include <iostream>
@@ -364,6 +364,10 @@ int main(){
} }
std::cout << "the use count is : " << age.use_count() << std::endl; std::cout << "the use count is : " << age.use_count() << std::endl;
if(age.unique()){
std::cout << "Congratulations! I am yours!" << std::endl;
}
// give up my ownership, it decreases the reference count of the managed object by one. // give up my ownership, it decreases the reference count of the managed object by one.
// if that shared pointer was the last owner (i.e., reference count becomes zero), the object is deleted. // if that shared pointer was the last owner (i.e., reference count becomes zero), the object is deleted.
// the shared_ptr itself is now empty (i.e., it holds nullptr). // the shared_ptr itself is now empty (i.e., it holds nullptr).

View File

@@ -15,6 +15,10 @@ int main(){
} }
std::cout << "the use count is : " << age.use_count() << std::endl; std::cout << "the use count is : " << age.use_count() << std::endl;
if(age.unique()){
std::cout << "Congratulations! I am yours!" << std::endl;
}
// give up my ownership, it decreases the reference count of the managed object by one. // give up my ownership, it decreases the reference count of the managed object by one.
// if that shared pointer was the last owner (i.e., reference count becomes zero), the object is deleted. // if that shared pointer was the last owner (i.e., reference count becomes zero), the object is deleted.
// the shared_ptr itself is now empty (i.e., it holds nullptr). // the shared_ptr itself is now empty (i.e., it holds nullptr).