Rocking the blogosphere

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

Effective C++ : 55 Specific Ways to Improve Your Programs and Designs (3rd Edition)
More Effective C++: 35 New Ways to Improve Your Programs and Designs
Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library
Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions
More Exceptional C++
C++ Coding Standards : 101 Rules, Guidelines, and Best Practices (C++ in Depth Series)
Thinking in C++, Volume 1: Introduction to Standard C++ (2nd Edition)
Thinking in C++, Vol. 2: Practical Programming, Second Edition
Modern C++ Design: Generic Programming and Design Patterns Applied

Popularity: 31% [?]

Comments

  1. March 14th, 2006 | 1:11 am

    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.

  2. Marc
    March 14th, 2006 | 10:48 am

    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++… :-)

  3. June 13th, 2006 | 5:46 pm

    What book would you suggest for someone just starting out in C++? I’m looking to start programming a little bit, haha.

  4. June 13th, 2006 | 10:23 pm

    “Thinking in C++” by Bruce Eckel is a pretty nice introductory C++ book.

  5. September 22nd, 2006 | 6:26 am

    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

Leave a reply