Default constructor for C + +

Source: Internet
Author: User
Tags constructor

1, each class must have a constructor, otherwise cannot create object;

2. If Programer does not provide any constructors, C + + provides a default constructor, which is an parameterless constructor that is responsible only for creating objects and not doing any initialization work;

3. C + + no longer provides the default default constructor as long as Programer defines a constructor (whether it is a parameterless or a parameter construct). That is, if you define a constructor with a parameter for a class, and you want a parameterless constructor, you must define it yourself;

4, like variable definitions, when you create an object with the default constructor, if you create a global object or a static object, the object's bit pattern is all 0, otherwise the object value is random.

Note: As described in 2, C + + In some cases provides a default constructor, but in some cases it is not invoked automatically.

In fact, when a user does not provide a custom constructor, declaring the object of the class and defining the array containing the object does not call the default constructor, but the default constructor is automatically invoked when the container containing the object is defined.

Examples are as follows:

#include <iostream>
#include <vector>
Using Std::cout;
Using Std::endl;
Using Std::vector;

Class Student
{
Public
Student (): Val (6) {}//1
Student (int val): Val (6) {}//2
int Val;
};

void Main ()
{
Student s;
cout << S.val; Compilation passed, but Run-time error: The variable ' s is being used without being defined.
Description does not call the default constructor

Student Arr[4];
cout << arr[2]; Compilation passed, but run error: the variable ' arr ' is being used without being.
Description does not call the default constructor

Vector<stu> VEC (5);
cout << vec[3].val << Endl; Run correctly, but the output is undefined (depending on the compiler, may be random or 0)
Description calls the default constructor

Student *ptr = new Student[4];
cout << Ptr[1].val; Run correctly, but the output is undefined (depending on the compiler, may be random or 0)
Description calls the default constructor

Student ss = Student (); Show Call
cout << Ss.val; Run correctly, but the output is undefined (depending on the compiler, may be random or 0)
Description calls the default constructor
}

If you remove annotation 1 from the student definition body, that is, a custom parameterless constructor, then the custom parameterless constructor will be invoked in several forms of object definitions in the main function;

But removing comment 2 only, that is, customizing a parameter construct without providing an parameterless construct, several forms of object definitions for the main function will have an error compiling because no default constructor is available.

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.