-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
41 lines (31 loc) · 1 KB
/
main.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
#include "./src/shared/shared.h"
#include "./src/weak/weak.h"
#include <iostream>
// A simple Node structure to demonstrate smart pointer usage
struct Node {
int value;
SharedPtr<Node> next;
WeakPtr<Node> prev;
Node(int val) : value(val) {
std::cout << "Node created with value " << value << std::endl;
}
~Node() {
std::cout << "Node with value " << value << " destroyed" << std::endl;
}
};
int main() {
// Create the head node with a unique pointer
SharedPtr<Node> head(new Node(1));
SharedPtr<Node> second = MakeShared<Node>(2);
head->next = second;
SharedPtr<Node> third = MakeShared<Node>(3);
second->next = third;
second->prev = head;
third->prev = second;
if (auto lockedPrev = third->prev.Lock()) {
std::cout << "The previous node to third has value: " << lockedPrev->value << std::endl;
} else {
std::cout << "The previous node has expired." << std::endl;
}
// Calling Destructors for pointers
}