diff --git a/lectures/27_garbage_collection/README.md b/lectures/27_garbage_collection/README.md index 370d025..7c40b28 100644 --- a/lectures/27_garbage_collection/README.md +++ b/lectures/27_garbage_collection/README.md @@ -344,7 +344,7 @@ int main() { ## 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 #include @@ -364,6 +364,10 @@ int main(){ } 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. // 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). diff --git a/lectures/27_garbage_collection/shared_ptr1.cpp b/lectures/27_garbage_collection/shared_ptr1.cpp index 4e99fe5..bbccee1 100644 --- a/lectures/27_garbage_collection/shared_ptr1.cpp +++ b/lectures/27_garbage_collection/shared_ptr1.cpp @@ -15,6 +15,10 @@ int main(){ } 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. // 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). @@ -23,6 +27,6 @@ int main(){ // question: what happens if we print age here: // std::cout << "age is " << *age << std::endl; - + return 0; }