-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lab_solution_13.cpp
62 lines (54 loc) · 1.44 KB
/
Lab_solution_13.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <memory>
using namespace std;
class Device
{
public:
Device()
{
cout << "Device Construct (Default) " <<endl;
}
Device(string model)
{
cout << "Device Construct " << model <<endl;
m_model = model;
}
~Device(){ cout << "Device Destruct " << m_model <<endl; }
std::string getModel() { return m_model; }
int getSystemID() { return m_systemID; }
bool getConnStatus() { return m_isConnected; }
void setModel(std::string const model) { m_model = model; }
void setSystemID(int systemID) { m_systemID = systemID; }
void setConnStatus(bool isConnected) { m_isConnected = isConnected; }
protected:
std::string m_model;
private:
int m_systemID = 0;
bool m_isConnected = false;
};
weak_ptr<Device> gDeviceObserver;
void observe()
{
cout << "Number of shared references = " << gDeviceObserver.use_count() << endl;
shared_ptr<Device> sPtr = gDeviceObserver.lock();
if (sPtr)
{
cout << sPtr.get()->getModel() << "Object is valid " << endl;
}
else
{
cout << "Object has expired\n";
}
}
int main()
{
cout << "Begin of main()" << endl;
{
shared_ptr<Device> devicePtr = make_shared<Device>("Device 1");
gDeviceObserver = devicePtr;
observe();
}
observe();
cout << "End of main()" << endl;
return 0;
}