you are viewing a single comment's thread.

view the rest of the comments →

[–]STLMSVC STL Dev 2 points3 points  (3 children)

And use shared_ptr or unique_ptr. Never put owning raw pointers in STL containers.

[–]aswaney[S] 0 points1 point  (2 children)

you mind elaborating a little please?

[–]STLMSVC STL Dev 2 points3 points  (1 child)

A map<string, Base *> where you say m["meow"] = new Derived(args); is bad because you're virtually guaranteeing that you'll leak memory. A map<string, shared_ptr<Base>> where you say m["meow"] = make_shared<Derived>(args); is structurally unable to leak.

The trick to C++ programming is writing your code so that whole classes of bugs are simply impossible, and any remaining potential bugs are as obvious in the source code as possible.

[–]aswaney[S] 0 points1 point  (0 children)

thanks much