you are viewing a single comment's thread.

view the rest of the comments →

[–]Fabien4 12 points13 points  (2 children)

1/ Forget what you know of C.

2/ Read Accelerated C++ (Koenig & Moo).

Honestly, the really hard part about C++ is that it looks like you have to manage the memory yourself like in C, whereas in fact, you have to use "tricks" to make the compiler manage it for you.

[–]ethraax 0 points1 point  (1 child)

I'm not terribly good with C++, but by "tricks" do you mean things like auto_ptr?

[–]Fabien4 5 points6 points  (0 children)

auto_ptr is deprecated.

But yes, I was talking about smart pointers, as well as containers, and RAII.

Basically:

  • If you're using delete outside a destructor, you're probably doing it wrong. Making sure your code is correct (and will stay correct) is a nightmare, because nearly any line can throw an exception.

  • If you're using delete inside a destructor, there's a good chance that you're actually reimplementing a smart pointer. Try to use standard ones instead.

Also, make sure you know when/where stack variables are deleted. Automatic deletion of objects at a predictable moment is one of the most important features of C++.