Thoughts from the office by Ed Ball
Monday, October 03, 2005

Effective C++, Third Edition, by Scott Meyers, contains a number of fundamental C++ guidelines that should be read by all C++ programmers. Having already read a previous edition of Effective C++, my expectation was that I would get a nice reminder of things I already knew. My hope was that I’d learn a few things that I didn’t already know, or had forgotten; here are some of those things:

  • You can use the member initialization list of a constructor definition even when you want to default construct a data member (p. 29).
  • A classic implementation of the copy assignment operator takes its argument by value (to make a copy of it) and then calls swap on that argument (p. 56).
  • Since compilers can reorder operations within a statement, very surprising things can happen in the face of exceptions (p. 76).
  • Prefer non-member non-friend functions to member functions (p. 98). I remember this controversial item from the last edition, but it was good to read his rationale again.
  • There are very peculiar rules about the swap function. You need to consult item 25 (p. 106) before you attempt to write a non-throwing swap function for your classes. Even calling the swap function is complicated – you must not call std::swap directly, but should instead call it like this: using std::swap; ... swap(obj1, obj2);
  • It never occurred to me to use an “interface class” for a single concrete class, merely to reduce compilation dependencies (p. 145).
  • Use using declarations to avoid hiding inherited names; you rarely want to hide inherited member functions when you add a new overload, but that’s what happens in C++ (p. 159).
  • Include a definition for a pure virtual function if you want to provide a default implementation that must be explicitly requested (p. 167).
  • I didn’t realize that “accessing names in templated base classes” was a problem; you’d think I’d have run into that before (item 43, p. 207).
  • I also never realized that I’d need to “define non-member functions inside templates when type conversions are desired.” The only way to do that is with a friend function, even if the function doesn’t need special access to your members (item 46, p. 222).
10/3/2005 9:23:51 AM (Pacific Daylight Time, UTC-07:00) | Comments [0] | Books#
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Search
Archive
Links
Categories
Administration
Blogroll