Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What C++ idioms should C++ programmers use? [closed]

What C++ idioms should C++ programmers know?

By C++ idioms, I mean design patterns or way of doing certain things that are only applicable for C++ or more applicable for C++ than most other languages.

Why one should use the idioms, and what do the idioms accomplish?

like image 937
Partial Avatar asked Nov 18 '09 22:11

Partial


5 Answers

By far the single most important "pattern" to learn and know that's (nearly) unique to C++ is RAII (Resource Acquisition Is Initialization).

Edit: (To answer extra question edited into the question). You use RAII primarily to (semi-)automate resource management. The most obvious use is freeing resources owned by objects when the owning objects go out of scope, such as freeing memory or closing files.

like image 194
Jerry Coffin Avatar answered Nov 02 '22 17:11

Jerry Coffin


Here is one list. If I had to pick a couple I might go with the Curiously Recurring Template Pattern or Virtual Contstructors.

like image 25
Duck Avatar answered Nov 02 '22 19:11

Duck


PIMPL, aka P ointer to IMPL ementation ?

like image 34
Dmitry Avatar answered Nov 02 '22 19:11

Dmitry


Template metaprogramming. It's great because it's basically compile-time duck typing, so you get most of the flexibility of duck typing with the speed of static typing.

like image 37
dsimcha Avatar answered Nov 02 '22 17:11

dsimcha


If you want to get the most out of the STL then iterators and functors/function objects are essential idioms. The use of iterators also implicitly relies on the 'half-open range' idiom too.

like image 7
the_mandrill Avatar answered Nov 02 '22 18:11

the_mandrill