**Autonomous driving tech./*C++

[C++] 03.17 review Unique_ptr

2wnswoo 2025. 3. 17. 08:42

Smart Pointer

 

메모리 관리를 자동화하고 안전하게 해주는 C++의 객체이다.

 

아래의 코드를 보자.

생성자 함수 앞에 ~이 붙어있다. 이것은,

구조체에서의 소멸자로, Desturctor는 ~를 붙인다. 소멸자는 객체가 삭제될 때 자동으로 호출 되는 함수이다.

#include <iostream>
#include <memory>

using namespace std;

class object {
public:
    object(){
        cout<<"출력1이 되었습니다."<<endl;
    }

   ~object(){
        cout<<"해제되었습니다."<<endl;    
    }  

    void function(){
        cout << "호출합니다." << endl;
    }
};

int main() {
    unique_ptr<object> smart_pointer(new object());  //스마트 포인터, unique_ptr 사용
    smart_pointer->function();

    return 0;
}

 

'**Autonomous driving tech. > *C++' 카테고리의 다른 글

[C++] 03.15 review <algorithm> sort(), stable_sort(), auto  (0) 2025.03.15
[C++] 03.14 review <cmath>, <vector>  (0) 2025.03.14
[C++] 03.04 review  (0) 2025.03.04
[C++] 03.03 review  (0) 2025.03.03
[C++] 03.02 review  (0) 2025.03.02