Special member functions in C++

Kris van Rens

90 minute session
beginner
intermediate
advanced
11:00-12:30, Wednesday, 28th June 2023

All class types in C++ have a set of non-static special member functions. Among them is the constructor, destructor, copy constructor and copy assignment operator. In C++11, move semantics were introduced, and the move constructor and move assignment operator were introduced. Special member functions, when not defined explicitly, may be automatically generated by the compiler. But this is not always the case. Exactly when are they defined, and when not? What is the rule of all or zero? And is it any good? Did you know there are about eight different forms of constructors? And when it comes to copy/move operations, do you know when you need to implement them yourself? And how? In this session, I will guide you through the wonderful world of special member functions. We will look into definition rules, constructor forms, copy/move operations, implementation idioms, and testing of special member functions. Also, we will take a look at some commonly encountered situations involving special member functions, such as class state initialization/reinitialization, and how to create consistent implementations for these cases. As a C++ software engineer equipped with a clear understanding of special member functions, you will be more confident and effective.

class types
special member functions
C++ fundamentals

Kris van Rens

Ever since the first time Kris got in touch with his dad's 1983 ZX Spectrum, he was captivated by the wonderful world of computer programming. In 1995, he learned to program 'Pacman' in x86 real-time assembly, which was soon followed by learning C and then C++, which came to be his bread and butter. He is very serious about code quality and is mostly interested in C++, Rust, Linux, programming languages/paradigms, software architecture and performance optimization. He currently works as the lead developer at ViNotion in Eindhoven (NL) and as a trainer at High Tech Institute Eindhoven. If he is not working, coding for fun or doing dad-/husband-related things, he is probably playing the guitar or running out in the woods. Kris studied mechatronics at FH Niederrhein, Krefeld (DE), and electrical engineering at the TU/e in Eindhoven (NL). He can be contacted at kris@vanrens.org