I’ve been using C++ for some time now and I still don’t feel very comfortable about using smart pointers and I’ve only been using them when editing some code that uses them, never in my own code (it might be worth to say that I’m a student).
Can you explain what are the types of smart pointers, how do they work and when to use them?
Also, what is the “protocol” when receiving or passing raw pointers in interfaces written by other people?
Thanks.
C++98 does not provide any smart pointers except
auto_ptrwhich is fraught with its own issues. C++0X tries to fix this by bringing in a few more varieties (shared_ptr,unique_ptretc.). In the meantime the best bet is to use Boost. Take a look at the various flavors available to you here. Boost is community driven, extensively tested and of course free. There is excellent documentation with sample code that will help you get started.There are a number of them. In short:
(That is from Boost documentation and note that they have containers for such pointers too!)
For me the most important rules are: