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: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: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
typedef s 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: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).