adding the unique function line
This commit is contained in:
committed by
JamesFlare1212
parent
45124db960
commit
284bd60029
@@ -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).
|
||||||
|
|||||||
@@ -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).
|
||||||
@@ -23,6 +27,6 @@ int main(){
|
|||||||
|
|
||||||
// question: what happens if we print age here:
|
// question: what happens if we print age here:
|
||||||
// std::cout << "age is " << *age << std::endl;
|
// std::cout << "age is " << *age << std::endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user