Skip to main content

Posts

Showing posts from July, 2024

What are Smart, Unique and Weak Pointer and when to use them

  Unique pointers  provide exclusive ownership of an object, preventing copying but allowing movement. They automatically release memory when the pointer goes out of scope, making them ideal for single ownership scenarios and resources like file handles. Shared pointers  enable multiple owners of an object through reference counting. While flexible, they can introduce performance overhead and potential circular reference issues. Careful consideration is essential when using shared pointers, especially in complex object structures. Weak pointers  do not own the object they reference but instead observe its lifetime. Primarily used to break circular dependencies between shared pointers, weak pointers provide a way to check if a shared pointer still exists without affecting ownership.