**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;
}