32. Be clear what kind of class you’re writing

Summary

Know thyself: There are different kinds of classes. Know which kind you are writing.

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 ...

Get C++ Coding Standards: 101 Rules, Guidelines, and Best Practices now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.