-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared_pointers.cpp
45 lines (36 loc) · 1.1 KB
/
shared_pointers.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
// g++ -std=c++11 -o shared_ptr shared_pointers.cpp
#include <iostream>
#include <memory>
class Entity
{
public:
Entity(){
std::cout << "Created an Entity" << std::endl;
}
~Entity(){
std::cout << "Deleted an Entity" << std::endl;
}
void PrintFunc(){
std::cout << "This is a print" << std::endl;
}
}; //end of class Entity
int main(){
{
std::shared_ptr<Entity> outside_entity;
{
std::shared_ptr<Entity> the_og_entity = std::make_shared<Entity>();
std::shared_ptr<Entity> copy_og_entity = the_og_entity;
outside_entity = the_og_entity;
outside_entity -> PrintFunc(); // calls the function of the_og_entity
std::shared_ptr<Entity> brand_new_entity = std::make_shared<Entity>();
}
}
}
/*
prints out
Created an Entity : std::shared_ptr<Entity> the_og_entity = std::make_shared<Entity>();
This is a print : outside_entity -> PrintFunc();
Created an Entity : std::shared_ptr<Entity> brand_new_entity = std::make_shared<Entity>();
Deleted an Entity : og
Deleted an Entity : new
*/