Personal tools
You are here: Home Blogs Weiyi Documents Effective C++ 2

Effective C++ 2

  • Item 28:  Partition the global namespace. 

  • Item 49:  Familiarize yourself with the standard library.

  • Item 47:  Ensure that non-local static objects are initialized before they're used.

  • Item 11:  Declare a copy constructor and an assignment operator for classes with dynamically allocated memory.

Item 28:  Partition the global namespace. 


If there are no name conflicts at the global level, clients of your library may find it cumbersome to use the fully qualified names. Fortunately, there is a way you can let them have their scopes and ignore them, too.

For your type names, provide typedefs that remove the need for explicit scoping. That is, for a type name T in your namespace-like struct S, provide a (global) typedef such that T is a synonym for S::T:


    typedef sdm::Handle Handle;
Functions are treated much like objects, but even though it's legal to define references to functions, future maintainers of your code will dislike you a lot less if you employ pointers to functions instead:


    sdm::Handle& (* const getHandle)() =      // getHandle is a
      sdm::getHandle;                         // const pointer (see
                                              // Item 21) to
                                              // sdm::getHandle
(If you're dying to know how to define a reference to a function, this should revitalize you:
    sdm::Handle& (&getHandle)() =      // getHandle is a reference
      sdm::getHandle;                  // to sdm::getHandle

Personally, I think this is kind of cool, but there's a reason you've probably never seen this before. Except for how they're initialized, references to functions and const pointers to functions behave identically, and pointers to functions are much more readily understood.)

Given these typedefs and references, clients not suffering from global name conflicts can just use the unqualified type and object names, while clients who do have conflicts can ignore the typedef and reference definitions and use fully qualified names. It's unlikely that all your clients will want to use the shorthand names, so you should be sure to put the typedefs and references in a different header file from the one containing your namespace-emulating struct.


Item 49:  Familiarize yourself with the standard library.

Mindful of the destructive power of rioting bands of incensed programmers, the standardization committee decided to create new header names for the std-wrapped components. The algorithm they chose for generating the new header names is as trivial as the results it produces are jarring: the .h on the existing C++ headers was simply dropped. So <iostream.h> became <iostream>, <complex.h> became <complex>, etc. For C headers, the same algorithm was applied, but a c was prepended to each result. Hence C's <string.h> became <cstring>, <stdio.h> became <cstdio>, etc. For a final twist, the old C++ headers were officially deprecated (i.e., listed as no longer supported), but the old C headers were not (to maintain C compatibility). In practice, compiler vendors have no incentive to disavow their customers' legacy software, so you can expect the old C++ headers to be supported for many years.

Practically speaking, then, this is the C++ header situation:

  • Old C++ header names like <iostream.h> are likely to continue to be supported, even though they aren't in the °official standard. The contents of such headers are not in namespace std

  • New C++ header names like <iostream> contain the same basic functionality as the corresponding old headers, but the contents of the headers are in namespace std. (During standardization, the details of some of the library components were modified, so there isn't necessarily an exact match between the entities in an old C++ header and those in a new one.)

  • Standard C headers like <stdio.h> continue to be supported. The contents of such headers are not in std

  • New C++ headers for the functionality in the C library have names like <cstdio>. They offer the same contents as the corresponding old C headers, but the contents are in std.


cin's real type is basic_istream<char> and string's is basic_string<char>.




Item 47:  Ensure that non-local static objects are initialized before they're used.


Document Actions