Learn How To Use C++ Defaulted Functions For Windows Development With C++ Builder

A defaulted function is a function that contains =default; in its prototype. This construction indicates that the function’s default definition should be used. Defaulted functions are a C++11 specific feature.

Defaulted functions example

By default, C++ provides four default special member functions. Users may override these defaults.

  • destructor
  • default constructor
  • copy constructor
  • copy assignment operator =

Also by default, C++ applies several global operators to classes. Users may provide class-specific operators.

  • sequence operator ,
  • address-of operator &
  • indirection operator *
  • member access operator ->
  • member indirection operator ->*
  • free-store allocation operator new
  • free-store deallocation operator delete

The management of defaults has several problems:

  • Constructor definitions are coupled; declaring any constructor suppresses the default constructor.
  • The destructor default is inappropriate to polymorphic classes, requiring an explicit definition.
  • Once a default is suppressed, there is no means to resurrect it.
  • Default implementations are often more efficient than manually specified implementations.
  • Non-default implementations are non-trivial, which affects type semantics, e.g. makes a type non-POD.
  • There is no means to prohibit a special member function or global operator without declaring a (non-trivial) substitute.

The most common encounter with these problems is when disabling copying of a class. The accepted technique is to declare a private copy constructor and a private copy assignment operator, and then fail to define either.

Head over and check out more information about defaulted functions on Windows in C++.