Skip to content

Prefer value semantics over reference semantics

Prefer value semantics over reference semantics, if there is no specific reason to use reference semantics. It makes things simpler by avoiding the complexity and common pitfalls of reference semantics, such as nullptr, dangling pointers, lifetime dependencies, ownership semantics, etc.1

In value semantics, data is passed around as values. Values are copied when passed into functions.

In reference semantics, objects are accessed via indirection (pointer or reference). The inherent risk of this that references can become invalidated.

  1. Citation from C++ Software Design, p406