-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared_ptr.cpp
More file actions
30 lines (22 loc) · 1.14 KB
/
shared_ptr.cpp
File metadata and controls
30 lines (22 loc) · 1.14 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
#include <iostream>
#include <memory>
class MyClass {
public:
MyClass() { std::cout << "Constructor is called." << std::endl; }
~MyClass() { std::cout << "Destructor is called." << std::endl; }
};
int main() {
// create a shared pointer to manage the MyClass object
//std::shared_ptr<MyClass> ptr1(new MyClass()); //This works too
std::shared_ptr<MyClass> ptr1 = std::make_shared<MyClass>();
{
// create another shared pointer and initialize it with the previously created pointer
std::shared_ptr<MyClass> ptr2 = ptr1;
std::cout << "Inside the inner scope. Refcount is "<< ptr2.use_count() << std::endl;
// both pointers share the same object, and the reference counter has been increased to 2
}
std::cout << "Outside the inner scope. Refcount is " << ptr1.use_count() << std::endl;
// leaving the inner scope will destroy ptr2, and the reference counter is decremented to 1
// the main function returns, ptr1 goes out of scope, and the reference counter becomes 0
// this causes the MyClass object to be deleted and the destructor is called
}