Question: 6. (Weak Pointers) A weak pointer is an observer of a shared pointer. It is useful as a way to avoid dangling pointers and also

6. (Weak Pointers)

A weak pointer is an observer of a shared pointer. It is useful as a way to avoid dangling pointers and also when we wish to use shared resources without assuming ownership.

Answer the following questions:

a) Create a shared pointer, print its use count and then create a weak pointer that observes it. Print the use count again. What are the values?

b) Assign a weak pointer to a shared pointer and check that the weak pointer is not empty.

c) Assign a weak pointer to another weak pointer; assign a weak pointer to a shared pointer. What is the use count in both cases?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting For Financial Instruments Questions!