-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.cpp
More file actions
73 lines (57 loc) · 1.77 KB
/
Example.cpp
File metadata and controls
73 lines (57 loc) · 1.77 KB
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
63
64
65
66
67
68
69
70
71
72
73
#include <managed_pointer.h>
#include <iostream>
#include <string>
using namespace std;
class Dog {
std::string _name;
public:
Dog(std::string name) : _name(name) { std::cout << _name << " is created" << std::endl; }
~Dog() { std::cout << _name << " is destroyed" << std::endl; }
std::string name() const { return _name; }
};
void print(const std::string& text) { std::cout << "> " << text << std::endl; }
int main() {
print("Creating Fido");
auto dog = make_managed<Dog>("Fido");
print("Replacing Fido with Rover via =");
dog = make_managed<Dog>("Rover");
print("Calling release()");
dog.release();
print("After release");
print("Creating Lassie");
dog = make_managed<Dog>("Lassie");
print("After creating Lassie");
print("Calling reset()");
dog.reset(new Dog("Spot"));
print("After reset");
print("Calling reset() with nullptr");
dog.reset(nullptr);
print("After reset");
auto* dogPtr = new Dog("Spike");
print("Creating Spike");
dog = managed_ptr<Dog>(dogPtr);
print("After creating Spike");
dog.reset();
print("After reset");
auto* dogPtr2 = new Dog("Dawg");
print("Creating Dawg");
dog = managed_ptr<Dog>(dogPtr2);
print("After creating Dawg");
dog.reset();
print("After reset");
print("Creating Snoopy");
dog = make_managed<Dog>("Snoopy");
dog.disable_delete();
dog.reset();
print("After reset");
// Untyped - can be deleted :)
print("Creating Scooby");
auto* dogPtr3 = new Dog("Scooby");
untyped_managed_ptr scooby = new managed_ptr<Dog>(dogPtr3);
print("After creating Scooby");
print("Deleting untyped Scooby");
delete scooby;
print("After delete");
print("returning...");
return 0;
}