In the C++ programming language, special member functions [1] are functions which the compiler will automatically generate if they are used, but not declared explicitly by the programmer. The automatically generated special member functions are:
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 [3] and the copy constructor and assignment operators will receive const&
parameters (and not be of the alternative legal forms). [4]
The following example depicts two classes: Explicit for which all special member functions are explicitly declared and Implicit for which none are declared.
#include<iostream>#include<string>#include<utility>classExplicit{public:Explicit(){std::cout<<"Default constructor "<<message_<<'\n';}explicitExplicit(std::stringmessage):message_(std::move(message)){std::cout<<"Non-default constructor "<<message_<<'\n';}Explicit(constExplicit&other){std::cout<<"Copy constructor "<<message_<<'\n';*this=other;// invoke copy assignment operator}Explicit&operator=(constExplicit&other){std::cout<<"Copy assignment operator "<<message_<<'\n';if(this!=&other){message_=other.message_;}return*this;}Explicit(Explicit&&other)noexcept{std::cout<<"Move constructor "<<message_<<'\n';*this=std::move(other);// invoke move assignment operator}Explicit&operator=(Explicit&&other)noexcept{std::cout<<"Move assignment operator "<<message_<<'\n';if(this!=&other){message_=std::move(other.message_);}return*this;}~Explicit(){std::cout<<"Destructor "<<message_<<'\n';}private:friendclassImplicit;std::stringmessage_;};classImplicit:publicExplicit{public:voidSpew(){std::cout<<"Implicit("<<message_<<", "<<member_.message_<<")\n";}private:Explicitmember_;};
Here are the signatures of the special member functions:
Function | syntax for class MyClass |
---|---|
Default constructor | MyClass(); |
Copy constructor | MyClass(const MyClass& other); |
Move constructor | MyClass(MyClass&& other) noexcept; |
Copy assignment operator | MyClass& operator=(const MyClass& other); |
Move assignment operator | MyClass& operator=(MyClass&& other) noexcept; |
Destructor | virtual ~MyClass(); |
In C++03 before the introduction of move semantics (in C++11) the special member functions [5] were:
Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types. This allows a function or class declaration to reference via a generic variable another different class without creating full declaration for each of these different classes.
C++ is a high-level, general-purpose programming language created by Danish computer scientist Bjarne Stroustrup. First released in 1985 as an extension of the C programming language, it has since expanded significantly over time; as of 1997, C++ has object-oriented, generic, and functional features, in addition to facilities for low-level memory manipulation. It is almost always implemented as a compiled language, and many vendors provide C++ compilers, including the Free Software Foundation, LLVM, Microsoft, Intel, Embarcadero, Oracle, and IBM.
A method in object-oriented programming (OOP) is a procedure associated with an object, and generally also a message. An object consists of state data and behavior; these compose an interface, which specifies how the object may be used. A method is a behavior of an object parametrized by a user.
In object-oriented programming such as is often used in C++ and Object Pascal, a virtual function or virtual method is an inheritable and overridable function or method that is dispatched dynamically. Virtual functions are an important part of (runtime) polymorphism in object-oriented programming (OOP). They allow for the execution of target functions that were not precisely identified at compile time.
In the C++ programming language, a copy constructor is a special constructor for creating a new object as a copy of an existing object. Copy constructors are the standard way of copying objects in C++, as opposed to cloning, and have C++-specific nuances.
In class-based, object-oriented programming, a constructor is a special type of function called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.
In the C++ programming language, a reference is a simple reference datatype that is less powerful but safer than the pointer type inherited from C. The name C++ reference may cause confusion, as in computer science a reference is a general concept datatype, with pointers and C++ references being specific reference datatype implementations. The definition of a reference in C++ is such that it does not need to exist. It can be implemented as a new name for an existing object.
In object-oriented programming, a destructor is a method which is invoked mechanically just before the memory of the object is released. It can happen when its lifetime is bound to scope and the execution leaves the scope, when it is embedded in another object whose lifetime ends, or when it was allocated dynamically and is released explicitly. Its main purpose is to free the resources which were acquired by the object during its life and/or deregister from other entities which may keep references to it. Use of destructors is needed for the process of Resource Acquisition Is Initialization (RAII).
A class in C++ is a user-defined type or data structure declared with any of the keywords class
, struct
or union
that has data and functions as its members whose access is governed by the three access specifiers private, protected or public. By default access to members of a C++ class declared with the keyword class
is private. The private members are not accessible outside the class; they can be accessed only through member functions of the class. The public members form an interface to the class and are accessible outside the class.
In computer programming languages, the term default constructor can refer to a constructor that is automatically generated by the compiler in the absence of any programmer-defined constructors, and is usually a nullary constructor. In other languages it is a constructor that can be called without having to provide any arguments, irrespective of whether the constructor is auto-generated or user-defined. Note that a constructor with formal parameters can still be called without arguments if default arguments were provided in the constructor's definition.
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 the publication year of the specification, though it was formerly named C++0x because it was expected to be published before 2010.
In the C++ programming language, the assignment operator, =
, is the operator used for assignment. Like most other operators in C++, it can be overloaded.
The rule of three and rule of five are rules of thumb in C++ for the building of exception-safe code and for formalizing rules on resource management. The rules prescribe how the default members of a class should be used to achieve these goals systematically.
In the C++ programming language, placement syntax allows programmers to explicitly specify the memory management of individual objects — i.e. their "placement" in memory. Normally, when an object is created dynamically, an allocation function is invoked in such a way that it will both allocate memory for the object, and initialize the object within the newly allocated memory. The placement syntax allows the programmer to supply additional arguments to the allocation function. A common use is to supply a pointer to a suitable region of storage where the object can be initialized, thus separating memory allocation from object construction.
In C++ computer programming, allocators are a component of the C++ Standard Library. The standard library provides several data structures, such as list and set, commonly referred to as containers. A common trait among these containers is their ability to change size during the execution of the program. To achieve this, some form of dynamic memory allocation is usually required. Allocators handle all the requests for allocation and deallocation of memory for a given container. The C++ Standard Library provides general-purpose allocators that are used by default, however, custom allocators may also be supplied by the programmer.
In C++ computer programming, copy elision refers to a compiler optimization technique that eliminates unnecessary copying of objects.
In computing, sequence containers refer to a group of container class templates in the standard library of the C++ programming language that implement storage of data elements. Being templates, they can be used to store arbitrary elements, such as integers or custom classes. One common property of all sequential containers is that the elements can be accessed sequentially. Like all other standard library components, they reside in namespace std.
C++14 is a version of the ISO/IEC 14882 standard for the C++ programming language. It is intended to be a small extension over C++11, featuring mainly bug fixes and small improvements, and was replaced by C++17. Its approval was announced on August 18, 2014. C++14 was published as ISO/IEC 14882:2014 in December 2014.
In the C++ programming language, the move assignment operator=
is used for transferring a temporary object to an existing object. The move assignment operator, like most C++ operators, can be overloaded. Like the copy assignment operator it is a special member function.
C++23 is the informal name for the next version of the ISO/IEC 14882 standard for the C++ programming language that will follow C++20. The final draft of this version is N4950.
&&
parameters instead of the alternatives.