forked from wadehuber/codeexamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmartptr2.cpp
More file actions
33 lines (25 loc) · 663 Bytes
/
Copy pathsmartptr2.cpp
File metadata and controls
33 lines (25 loc) · 663 Bytes
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
#include<iostream>
#include<memory>
using namespace std;
class MyClass {
private:
int x;
public:
explicit MyClass(int n=1) : x(n) { cout << "Constructor x=" << n << endl; }
~MyClass() { cout << "Destructor x=" << x << endl; }
void print() { cout << "MyClassObject x=" << x << endl; }
};
MyClass * myClassMakerPtr(int n) {
return new MyClass(n);
}
unique_ptr<MyClass> myClassMakerSmart(int n) {
return make_unique<MyClass>(n);
}
int main() {
auto mPtrRaw = myClassMakerPtr(5);
auto mPtrSmart = myClassMakerSmart(10);
mPtrRaw->print();
mPtrSmart->print();
delete(mPtrRaw);
return 0;
}