Well I know there are good topics about smart pointers, but since I'm starting to use them extensively I want to know some recommendations on bad or dangerous uses, e.g:
- Raw-pointer conversions
- Smart pointers to references/References to smart pointers
- etc.
Thank you in advance.
(Added)
Suppose I've a class A with pointers to B as members, I can specify them as B* (raw-ptr) or as shared_ptr members.
class A
{
B* ptr_b;
shared_ptr<B> s_ptr_b;
}
If I don't understood bad,
ptr_b = new (B); // raw pointer of course
ptr_b = shared_ptr<B>(new B) ; // safe, ownership to shared_ptr
s_ptr_b = new (B); // unsafe due to conversion from B* to shared_ptr<B>
s_ptr_b = shared_ptr<B>(new B) ; // also safe, equal to case II?
[Added more...]
- Altough user Joe posted that the 3rd conversion was safe (
X* to shared_ptr<X>
), it's not safe and it is forbidden.
I'm left with some advices regarding:
Passing
shared_ptr<X>
to functions expectingX*
If
X* x = shared_ptr<X>(new X)
equals toshared_ptr<X> x = shared_ptr<X>(new X).
Interaction between references and shared_ptr<>.
Thank you very much.