C++ tips
Under construction
- Think about using composition before using inheritance
- Leverage STL and Boost
- With STL, use iterators to write container-independent code
- Known the different STL iterator concepts - e.g..: InputIterator, OutputIterator, ForwardIterator, BidirectionalIterator, and RandomAccessIterator.
- Use iterator_traits to determine the capabilities of iterators and to write template specializations that are efficient.
- Always catch exceptions by reference to avoid “slicing”
- #include guards
- The explicit keyword for 1 argument constructors can prevent confusing automatic conversions
- The destructor should generally be virtual
- Use smart pointers and the RAII idiom
- Use the pimpl (a.k.a.: “dptr” or “Cheshire Cat”) idiom
- Prefer C++ style casts
- Prefer use of const to #define
Popularity: 31% [?]












You’re speaking “Scott Meyers” here, so be proud. One caveat:
“The destructor should generally be virtual”
I think a better rule is:
“If any method is virtual, your destructor should be virtual.”
A non-virtual destructor is fine if you don’t have derived classes. Even if you do have derived classes, so long as noone would reasonably be invoking a delete a pointer to your base class, there isn’t much of a problem. Unless there is a virtual method, not much point in invoking methods on the base class.
Sage advice.
I’d forgotten about this little page. It would be nice to add more stuff to it, but I’m getting rusty with C++…
What book would you suggest for someone just starting out in C++? I’m looking to start programming a little bit, haha.
“Thinking in C++” by Bruce Eckel is a pretty nice introductory C++ book.
For real C++ newbies, I recommend “Accelerated C++” by Andrew Koenig & Barbara Moo
See its review by ACCU:
http://accu.org/index.php/book_reviews?url=view.xqy?review=a002212&term=accelerated
Also, I’d suggest to start learning C++ from books marked as Highly Recommended in ACCU’s review. These reviews are very helpful guidelines and usually made by C++ gurus.
Cheers