Quick Q: Does using std:: smart pointers mean not using raw pointers? -- StackOverflow
Quick A: No. Smart pointers are for ownership. Raw pointers are still fine for non-owning pointers to an object that outlives the pointer.
C++11 Smart Pointer Semantics
I've been working with pointers for a few years now, but I only very recently decided to transition over to C++11's smart pointers (namely unique, shared, and weak). I've done a fair bit of research on them and these are the conclusions that I've drawn:
- Unique pointers are great. They manage their own memory and are as lightweight as raw pointers. Prefer
unique_ptrover raw pointers as much as possible.- Shared pointers are complicated. They have significant overhead due to reference counting. Pass them by const reference or regret the error of your ways. They're not evil, but should be used sparingly.
- Shared pointers should own objects; use weak pointers when ownership is not required. Locking a
weak_ptrhas equivalent overhead to theshared_ptrcopy constructor.- Continue to ignore the existence of
auto_ptr, which is now deprecated anyhow.So with these tenets in mind, I set off to revise my code base to utilize our new shiny smart pointers, fully intending to clear to board of as many raw pointers as possible. I've become confused, however, as to how best take advantage of the C++11 smart pointers. ...


Trust Eric Niebler to deliver the goods -- when it comes to insightful discussion, cool code, and shameless puns:
Do you know a beginner who'd like to learn C++? Or even just learn how to program... using C++?
A few hours ago,