C++ Special Member Function Guidelines
The C++ special member functions include default constructor, copy constructor, move constructor, destructor, copy assignment, and move assignment. These functions can be defaulted, deleted, or have a custom implementation, and most classes will fit into one of four common patterns. The rule of zero suggests that when in doubt, developers should follow the normal class pattern and use default implementations for special member functions.
- ▪C++ special member functions can be defaulted, deleted, or custom implemented.
- ▪The rule of zero is a guideline for implementing special member functions in C++ classes.
- ▪There are four common patterns for implementing special member functions in C++ classes.
Opening excerpt (first ~120 words) tap to expand
C++ Special Member Function Guidelines The C++ special member functions are: default constructor copy constructor move constructor destructor copy assignment move assignment They can be either = defaulted, = deleted, not manually written at all, or have a custom implementation. The majority of classes you write will match one of the four options listed below. When in doubt, do what normal does (rule of zero). class normal { public: normal(); // if it makes sense ~normal() = default; normal(const normal&) = default; normal(normal&&) = default; normal& operator=(const normal&) = default; normal& operator=(normal&&) = default; }; class immoveable { public: immoveable(); // if it makes sense ~immoveable() = default; immoveable(const immoveable&) = delete; immoveable& operator=(const…
Excerpt limited to ~120 words for fair-use compliance. The full article is at Foonathan.