A default constructor is a constructor which can be called with no arguments (either defined with an empty parameter list, or with default arguments provided for every parameter). A type with a public default constructor is DefaultConstructible. Show
Contents
[edit] Syntax
Where class-name must name the current class (or current instantiation of a class template), or, when declared at namespace scope or in a friend declaration, it must be a qualified class name. [edit] Explanation1) Declaration of a default constructor inside of class definition. 2) Definition of the constructor outside of class definition (the class must contain a declaration (1)). See constructors and member initializer lists for details on the constructor body. 3) Deleted default constructor: if it is selected by overload resolution, the program fails to compile. 4) Defaulted default constructor: the compiler will define the implicit default constructor even if other constructors are present. 5) Defaulted default constructor outside of class definition (the class must contain a declaration (1)). Such constructor is treated as user-provided (see below and value initialization). Default constructors are called during default initializations and value initializations. [edit] Implicitly-declared default constructorIf no
user-declared constructors of any kind are provided for a class type (struct, class, or union), the compiler will always declare a default constructor as an
The implicitly-declared (or defaulted on its first declaration) default constructor has an exception specification as described in dynamic exception specification (until C++17)exception specification (since C++17). [edit] Implicitly-defined default constructorIf the implicitly-declared default constructor is not defined as deleted, it is defined (that is, a function body is generated and compiled) by the compiler if odr-used or needed for constant evaluation (since C++11), and it has the same effect as a user-defined constructor with empty body and empty initializer list. That is, it calls the default constructors of the bases and of the non-static members of this class. Class types with an empty user-provided constructor may get treated differently than those with an implicitly-defined or defaulted default constructor during value initialization.
[edit] Deleted implicitly-declared default constructorThe implicitly-declared or defaulted (since C++11) default constructor for class
[edit] Trivial default constructorThe default constructor for class
A trivial default constructor is a constructor that performs no action. All data types compatible with the C language (POD types) are trivially default-constructible. [edit] Eligible default constructor
Triviality of eligible default constructors determines whether the class is an implicit-lifetime type, and whether the class is a trivial type. [edit] Examplestruct A { int x; A(int x = 1): x(x) {} // user-defined default constructor }; struct B: A { // B::B() is implicitly-defined, calls A::A() }; struct C { A a; // C::C() is implicitly-defined, calls A::A() }; struct D: A { D(int y): A(y) {} // D::D() is not declared because another constructor exists }; struct E: A { E(int y): A(y) {} E() = default; // explicitly defaulted, calls A::A() }; struct F { int& ref; // reference member const int c; // const member // F::F() is implicitly defined as deleted }; // user declared copy constructor (either user-provided, deleted or defaulted) // prevents the implicit generation of a default constructor struct G { G(const G&) {} // G::G() is implicitly defined as deleted }; struct H { H(const H&) = delete; // H::H() is implicitly defined as deleted }; struct I { I(const I&) = default; // I::I() is implicitly defined as deleted }; int main() { A a; B b; C c; // D d; // compile error E e; // F f; // compile error // G g; // compile error // H h; // compile error // I i; // compile error } [edit] Defect reportsThe following behavior-changing defect reports were applied retroactively to previously published C++ standards.
[edit] See also
Which function is automatically called in C plus plus if we do not define it in a class?A constructor in C++ is a special method that is automatically called when an object of a class is created.
Is destructor called automatically in C++?A destructor is a member function that is invoked automatically when the object goes out of scope or is explicitly destroyed by a call to delete .
Which function in C++ is define to do nothing?getVal() << std::endl; return 0; }
Which is automatically added to every class in C++?Which of the followings is/are automatically added to every class, if we do not write our own. Explanation: In C++, if we do not write our own, then compiler automatically creates a default constructor, a copy constructor and a assignment operator for every class.
How do you call a function inside a class in C++?To call some function before main() method in C++,. Create a class.. Create a function in this class to be called.. Create the constructor of this class and call the above method in this constructor.. Now declare an object of this class as a global variable.. |