blob: a574d4561125d93a79ea66846800d293597a8f5a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#include "shared_pointer.hh"
int main()
{
// Lifetime of the data
{
SharedPointer<int> p2;
{
SharedPointer<int> p1{ new int{ 5 } };
p2 = p1; // Now both SharedPointers own the memory
}
// p1 is freed, but the int pointer remains
// Memory still exists, due to p2
std::cout << "Value of p2: " << *p2 << std::endl; // Output: 5
}
// p2 is freed, and since no one else owns the memory,
// the int pointer is freed too
return 0;
}
|