All submissions

Access Object with weak_ptr

#include <iostream>
#include <memory>
using namespace std;

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

int main() {
	// your code here
	// Create a shared_ptr<Device> .
	// Create a weak_ptr<Device> that observes the above object.
    shared_ptr<Device> p{new Device};
    weak_ptr<Device> wp{p};
 
    if (auto temp = wp.lock()) {
        temp->status();
    }
    return 0;
}
Loading...

Input

Expected Output

Device is active