-
Notifications
You must be signed in to change notification settings - Fork 0
/
smart_pointer.cpp
43 lines (35 loc) · 945 Bytes
/
smart_pointer.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
/*
Ref: https://docs.microsoft.com/en-us/cpp/cpp/smart-pointers-modern-cpp?view=msvc-170
Unique pointer -> constructor needs to be explicitly called.
Shared pointer
*/
#include <iostream>
#include <string>
#include <memory>
class Entity {
public:
Entity() {
std::cout << "Created Entity\n";
}
~Entity() {
std::cout << "Destroyed Entity! \n";
}
void Print() {}
};
int main() {
std::cout << "Unique ptr" << std::endl;
{
// std::unique_ptr<Entity>entity(new Entity()); // one way to declare
std::unique_ptr<Entity>entity = std::make_unique<Entity>(); // preferred way
entity->Print();
}
// Shared ptr
// Works by the logic of reference counting -> keep a count of how many references are made to the ptr.
std::cout << "---------\nShared ptr" << std::endl;
std::shared_ptr<Entity>e0; // shared ptr has overhead.
{
std::shared_ptr<Entity>sharedEntity = std::make_shared<Entity>();
e0 = sharedEntity;
}
return 0;
}