C + + Note 018: Calling rules for constructors

Source: Internet
Author: User

 original notes, reproduced please specify the source!

Click "Focus", attention is also a virtue ~

First, the default constructor

Two special constructors

1. default no parameter constructor

When a constructor is not defined in a class, the compiler provides an parameterless constructor by default, and its function body is empty;

2 , Default copy constructor

When a copy constructor is not defined in a class, the compiler defaults to providing a default copy constructor, which simply replicates the value of the member variable.

Second, constructor call rules

1. When no one constructor is defined in the class, the C + + compiler provides a default parameterless constructor and a default copy constructor;

2. When a copy constructor is defined in a class, the C + + compiler does not provide a parameterless constructor;

3. When an arbitrary non-copy constructor is defined in a class (that is, when an parameterless constructor or a parametric constructor is provided in a class), the C + + compiler does not provide a parameterless constructor;

4, the default copy constructor member variable simple assignment value.

three . Case Analysis

1. No definition when you construct a function C + + calls the default parameterless constructor (compiled by)

#include <iostream>

using namespace Std;

Class Function

{

Private

int A;

int b;

};

int main ()

{

Function T1;

System ("pause");

return 0;

}

In the above program, we do not define constructors, compile through, C + + automatically call the default parameterless constructor.

2 , define a parameter constructor Function , C + + does not provide a default parameterless constructor

#include <iostream>

using namespace Std;

Class Function

{

Defining a parameter constructor

Function (int x,int y)

{

A=x;

B=y;

}

Private

int A;

int b;

};

int main ()

{

Function T1;

System ("pause");

return 0;

}

Looking at the above program, we define an argument constructor, but when we define the object we do not assign it as a constructor parameter, this time the compiler will look for the default parameterless constructor, but since we have manually added the parameter constructor, the compiler no longer provides the parameterless default constructor, so the compilation fails!

However, if we define an object and provide a call that corresponds to the form of a parameter constructor, then it can be compiled. The copy constructor is also consistent. The following programs:

#include <iostream>

using namespace Std;

Class Function

{

Public

Defining a parameter constructor

Function (int x,int y)

{

A=x;

B=y;

}

Private

int A;

int b;

};

int main ()

{

Function T1 (UP);

System ("pause");

return 0;

}

four . Summary

1. If you do not write the constructor by hand, the default parameterless constructor will be called automatically;

2. If you write your own constructor (whether it is a parametric constructor or a copy constructor), then the compiler will no longer provide a default parameterless constructor, and when you define an object, you pass parameters in the form of a constructor parameter. (to be added)

Construction, Periodic summary of the destruction:

(1) A special function for initializing the state of an object in C + + when the constructor is constructed;

(2) The constructor is called automatically when the object is created;

(3) Constructors and ordinary member functions follow overloaded rules;

(4) The important guarantee of the correct initialization of the object when copying the constructor;

(5) When necessary, the copy constructor must be written manually.

Original notes, reproduced please specify the source!

More wonderful please pay attention to the public number: programming according to Law

Long Press to unlock

Unlock more Insider Stories

Technology-driven Life

C + + Note 018: Calling rules for constructors

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.