• 코드:
​x
 
1
#include <iostream>
2
#include <memory>
3
using namespace std;
4
​
5
class Person
6
{
7
private:
8
    string name_;
9
    int age_;
10
public:
11
    Person(const string& name, int age);    // 기초 클래스 생성자의 선언 
12
    ~Person() { cout << "소멸자가 호출되었습니다." << endl; }
13
    void ShowPersonInfo();
14
};
15
​
16
int main(void)
17
{
18
    shared_ptr<Person> hong = make_shared<Person>("길동", 29);
19
    cout << "현재 소유자 수 : " << hong.use_count() << endl;  // 1
20
    auto han = hong;
21
    cout << "현재 소유자 수 : " << hong.use_count() << endl;  // 2
22
    han.reset();        // shared_ptr인 han을 해제함. 
23
    cout << "현재 소유자 수 : " << hong.use_count() << endl;  // 1
24
    return 0;
25
}
26
​
27
Person::Person(const string& name, int age) // 기초 클래스 생성자의 정의 
28
{
29
    name_ = name;
30
    age_ = age;
31
    cout << "생성자가 호출되었습니다." << endl;
32
}
33
​
34
void Person::ShowPersonInfo()
35
{
36
    cout << name_ << "의 나이는 " << age_ << "살입니다." << endl;
37
}
표준입력 & 실행옵션