Discussion
Team LiB
Previous Section Next Section

Discussion

Different kinds of classes serve different purposes, and so follow different rules. Value classes (e.g., std::pair, std::vector) are modeled after built-in types. A value class:

  • Has a public destructor, copy constructor, and assignment with value semantics.

  • Has no virtual functions (including the destructor).

  • Is intended to be used as a concrete class, not as a base class (see Item 35).

  • Is usually instantiated on the stack or as a directly held member of another class.

Base classes are the building blocks of class hierarchies. A base class:

  • Has a destructor that is public and virtual or else protected and nonvirtual (see Item 50), and a nonpublic copy constructor and assignment operator (see Item 53).

  • Establishes interfaces through virtual functions.

  • Is usually instantiated dynamically on the heap and used via a (smart) pointer.

Loosely, traits classes are templates that carry information about types. A traits class:

  • Contains only typedefs and static functions. It has no modifiable state or virtuals.

  • Is not usually instantiated (construction can normally be disabled).

Policy classes (normally templates) are fragments of pluggable behavior. A policy class:

  • May or may not have state or virtual functions.

  • Is not usually instantiated standalone, but only as a base or member.

Exception classes exhibit an unusual mix of value and reference semantics: They are thrown by value but should be caught by reference (see Item 73). An exception class:

  • Has a public destructor and no-fail constructors (especially a no-fail copy constructor; throwing from an exception's copy constructor will abort your program).

  • Has virtual functions, and often implements cloning (see Item 54) and visitation.

  • Preferably derives virtually from std::exception.

Ancillary classes typically support specific idioms (e.g., RAII; see Item 13). They should be easy to use correctly and hard to use incorrectly (e.g., see Item 53).

    Team LiB
    Previous Section Next Section