What is a smart pointer and when should I use it in C++?


A smart pointer is a class that wraps a 'raw' (or 'bare') C++ pointer. It is used to manage resources the pointer points to. For example, if the reference to that memory location is lost. It kind of acts like a garbage collector. There are multiple smart pointer types.


You should almost always use a smart pointer. This is because the main pain point of using pointers is manual memory management and memory leaks. The smart pointer tries to get rid of both of these. If you don't want to do either of these in practice, you should use a smart pointer.

Updated on: 30-Jul-2019

276 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements