There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.
If this question can be reworded to fit the rules in the help center, please edit the question.

1

What C++ books have you read, or are reading?
–
Fred NurkFeb 1 '11 at 10:39

6 Answers
6

The pitfalls

There are so many pitfalls in C++, that if you don't know them you will create very unstable code, with tons of memory leaks and buffer overruns. Compared to more modern languages with garbage collection, you must release all memory yourself. Also, the code is very low-level. There is nothing preventing you from overwriting your own program code (which has been exploited by many IE hacks).

So the next you must learn are the programming practices that mitigate these risks, e.g. using smart pointers to handle freeing objects, wrapping byte arrays in classes handling the data, etc.

"Accelerated C++" by Koenig and Moo is a good book for new C++ programmers, as it introduced templates and classes by showing examples of use first, then design and creation. I found it easier to understand why to use them with that approach.
–
Larry ColemanFeb 1 '11 at 13:30

I've started reading Effective C++, and it really does help a lot. Thank you !
–
NiphraFeb 1 '11 at 16:31

2

"There is nothing preventing you from overwriting your own program code" Huh? What? This isn't the fault of the language - indeed, in most operating systems this isn't actually doable unless you actively take steps to make it possible. And poorly written programs are potentially exploitable regardless of the language they're written in. Yes, C++ is low-level and, yes, it has its flaws, but let's not go overboard with nonsense like that.
–
Nik BougalisNov 17 '12 at 4:46

@NikBougalis The OS prevents it -- in cases where the OS prevents it -- but C/C++, by design, gives you a method of doing it, in cases where the OS doesn't prevent it; whereas many other languages don't. So it's not really nonsense to characterise this as a language issue -- but I agree the original point is rather overstated.
–
greggoSep 5 '14 at 16:50

4

@greggo it is nonsense - one may as well complain that a coffee mug doesn't protect you from accidentally pouring salt in your coffee. The fact is this: C and C++ don't know anything about your program or the system it's running on. The language could not possibly address the "zomg! my program was overwritten" concern without us first fundamentally altering the underlying assumptions on which the language is built.
–
Nik BougalisSep 6 '14 at 0:15

Idioms

C++ supports a great variety of features. Likewise, one can be easily tempted to use it in many different ways or styles. Unfortunately, it doesn't suit many of them well - and thus becomes tedious, error-prone or slow if in such manner.

Or phrased differently, without really knowing what you do, it's incredibly easy to use C++ in a wrong way (which would much better supported in other languages). Plain learning by doing can therefore lead in a wrong direction - so reading good books is probably more important than in other languages (see this post on the same topic).

Java for example is an inherently object-oriented language. You can't program it much different that. In C++ however, you can. You can use it like you'd do in C or Java - and neither way is what C++ excels in, so you'd better stay with C or Java in these cases.

Therefore, you really need to know the proper style and idioms to get C++ right, right from the beginning. Unfortunately, they can be quite complex at first, but here are some - from simple to advanced.

Use const

Don't worry about micro-optimizations (should I inline a function, << or * etc.?)

Refrain from using raw pointer or arrays wherever possible (especially void*). Use references or smart pointers

Thanks for the advices. I don't really get why I should avoid writing object-oriented code, though; or did you mean some parts of OOP specifically?
–
NiphraFeb 1 '11 at 16:36

1

OOP does not solely mean programming with objects, it implies certain design-patterns, runtime polymorphism through inheritance, class hierarchies ... C++ is not really great in expressing these - you need (smart) pointers and often explicit memory management for runtime polymorphism, it's relatively slow and full of pitfalls (ever forgot a virtual destructor?). You often have cleaner ways of expressing the same with pure objects and e.g. generic programming / static polymorphism.
–
DarioFeb 1 '11 at 18:52

2

@Niphra: it's not about not using OOP, it's about not restraining yourself to OOP. C++ offers multiple paradigms, and good C++ programs use most, if not all, of them.
–
Matthieu M.Feb 1 '11 at 18:55

C++'s power is that it's an awesome language. C++'s drawback is that it's an awesome language. It can be both awe-ful and awful at the same time.

If you want to learn it -- and there's no reason you shouldn't -- you will need to learn it well. You will need to read a lot. There are a few books that will help you on your way: Accelerated C++ will get you started, especially if you do the exercises. Effective C++, Effective STL, Exceptional C++, and their continuing series are among the best in the game for bite-size hints of how to improve your ways of working.

I think one thing that will help along your road of good code is experience. For now, just get stuck into the language and don't worry too much about "best coding practices" until you are comfortable enough with it to start looking at ways to improve your usage. You will learn to recognise what's good and what's bad. Once you have the language down, then you can look back and think "ok, I should have implemented this differently", or "this looks messy, how can I improve it?"

I think about it like this: If you learn a new spoken language, you don't dive straight in and learn every single grammatical rule before you start learning simple words or how to say "where is the bus stop?". Programming (IMO) is like a second language (or third, fourth, etc). Treat it like you would any other language and learn by doing. The rest will come with time.

I've often read it's easier to write
bad code in C++ than in most
languages, and that it is a lot deeper
than what it seems

clarification: in C++ it is easier to write bad code unintentionally than in other languages. You can write bad code in any language but in C++ you set out with the best of intentions but often end up somewhere unexpected because C++ - after you move from "novice" to say "advanced beginner" you get the false impression that C++ is easier than it actually is. But C++ is evolving and all new template libraries make your life easier (and safer).

This also depends on how you learn C++. If you start learning it as an enhanced C, you're more likely to get yourself into trouble at that stage than if you just learn it as C++.
–
David ThornleyFeb 1 '11 at 15:57

Time for the obligatory Stroustrup quote: "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off"
–
MawgJun 3 at 11:38