47. Access Object with weak_ptr

We have already defined a class Device with:

void status() { cout << "Device is active\n"; }

Your task is to:

  1. Create a shared_ptr<Device> in main().
  2. Create a weak_ptr<Device> that observes the same object.
     

The program will already handle converting the weak_ptr into a shared_ptr with .lock() and calling status().

 

Example

Output:

Device is active

 

Why this output?

Because the weak_ptr observes the object managed by shared_ptr, and the program locks it to safely call status().

 

Question Significance

This shows how weak_ptr is linked to a shared_ptr, but does not extend the lifetime of the object.

Loading...

Input

Expected Output

Device is active