0

Possible Duplicate:
What is a smart pointer and when should I use one?

I've recently read about smart pointer, such as shared_ptr or auto_ptr. Is it good/bad practice to prefer them over normal pointers?

E.g. should I use them in arguments to functions or when storing pointers as members of a class?

Community
  • 1
  • 1
Erik
  • 11,944
  • 18
  • 87
  • 126
  • Recently read about them? Scott Meyer wrote about them back in 1995, so they've stood the test of time. – duffymo Jul 14 '12 at 13:30

1 Answers1

0

They are great idea. They are slower than normal pointers. But, without knowing the whole pointer life cycle or just to be on the safe side, it better to use smart pointer.
As to a an argument of a function, if the function either stores the pointer or leads to its deletion, it's better to use smart pointer. Otherwise normal pointer is good too.

Daniel
  • 30,896
  • 18
  • 85
  • 139
  • 1
    They are not slower than normal pointers. – rubenvb Jul 14 '12 at 13:32
  • 1
    @rubenvb: they add non-nop operations to copy, construct and release and thus the program will be slower. That doesn't say it will be much slower, it can even be undetected. – Daniel Jul 14 '12 at 13:34
  • 1
    @Dani: `shared_ptr` does add operations, but `unique_ptr` (or `auto_ptr` if you're stuck in the past) has no overhead over a raw pointer. – Mike Seymour Jul 14 '12 at 13:48