HOME

TheInfoList



OR:

Special member functions in C++ are functions which the
compiler In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs that ...
will automatically generate if they are used, but not declared explicitly by the programmer. The automatically generated special member functions are: * Default constructor if no other constructor is explicitly declared. *
Copy constructor In class-based, object-oriented programming, a constructor (abbreviation: ctor) is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required ...
if no move constructor and move assignment operator are explicitly declared. : If a destructor is declared generation of a copy constructor is deprecated (C++11, proposal N3242). *
Move constructor In class-based, object-oriented programming, a constructor (abbreviation: ctor) is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required ...
if no copy constructor, copy assignment operator, move assignment operator and destructor are explicitly declared. * Copy assignment operator if no move constructor and move assignment operator are explicitly declared. : If a destructor is declared, generation of a copy assignment operator is deprecated. * Move assignment operator if no copy constructor, copy assignment operator, move constructor and destructor are explicitly declared. * Destructor * The 'address of' operator (unary '&' operator) In these cases the compiler generated versions of these functions perform a ''memberwise'' operation. For example, the compiler generated destructor will destroy each sub-object (base class or member) of the object. The compiler generated functions will be public, non- virtual and the copy constructor and assignment operators will receive const& parameters (and not be of the alternative legal forms).


Example

The following example depicts two classes: for which all special member functions are explicitly declared and for which none are declared. #include #include #include class Explicit ; class Implicit : public Explicit ;


Signatures

Here are the signatures of the special member functions:


C++03

In C++03 before the introduction of
move semantics C++11 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and was later replaced by C++14. The name follows the tradition of naming language versions by ...
(in C++11) the special member functions were: * Default constructor (if no other constructor is explicitly declared) *
Copy constructor In class-based, object-oriented programming, a constructor (abbreviation: ctor) is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required ...
* Copy assignment operator * Destructor * The 'address of' operator (unary '&' operator)


References

{{C++ programming language C++ Articles with example C++ code