The role of the default constructor for the "C + + Primer seventh chapter"

Source: Internet
Author: User

The role of the default constructor

1. Default constructors

A C + + default constructor is a constructor that provides a default value for a parameter in a class, typically an empty function with no parameter value, or a constructor that provides default values, and if the user does not define a constructor, the compiler gives the class a default constructor. But as long as the user has customized any constructor, then the compiler will not provide a default constructor , in this case, easy to compile error, so the correct way to do is to define the constructor, the user also need to add a default constructor, so that does not cause compilation error.

such as: User-defined default constructors

class test{public:    //  default constructor};

2. Why to add a default constructor

The reasons are as follows:

1, when the user defines an array, and the element type of the array is a Class object 1, then this time will call the default constructor, if this time object 1 does not define the default constructor, will be an error. However, if it is not an object type, it is the built-in data type of C + + and will not error.

such as: Object array[10];

2, when the user defines an array and dynamically allocates object 1 with new, then this time if object 1 does not have a default constructor, it will also be an error, because new will invoke object 1 's parameterless default constructor to initialize the object.

such as: Object *temp = new OBJECT[10];

3, when the user uses the standard library container, when the container is a class, such as objects, this time will call the object's default constructor to initialize, if the object's class does not define a default constructor, then will be an error.

such as:vector<object> Vo;

4. When a Class A is a member of an object of another class B, if a provides a parameterless constructor and B does not, then a cannot use its own parameterless constructor.

Example 1:

The following code causes a compilation error.

class b{    B (int  i) {}}; class a{    A () {}    b b;}; int Main (void) {     //  error C2512: ' B ': no appropriate default Constructor available    return0

Example 2:

1 classNoDefault2 {3  Public:4NoDefault (CONSST std::string&);//cannot synthesize default constructor5     //There are other members, but no other constructors6 };7 8 structA9 {Ten NoDefault My_men;  One }; A  -A;//error: cannot be a composite constructor -  the structB - { -B () {}//error, B_member no initial value - NoDefault B_member; +  -};

5, if the class A defines a copy constructor, but does not define a default constructor, then if B inherits a, a, a, a, a, a, a, the initialization will call A's default constructor, this time will be an error.

class a{    A (const a&) {}}; class  Public a{    }; int Main (void) {     //error C2512: ' B ': no appropriate default Constructor available    return0
Resources

C++ Importance of default constructors

The role of the default constructor for the "C + + Primer seventh chapter"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.