Learn about the new ---- talk about the constructor again (turn: no turn)

Source: Internet
Author: User

Learn more
---- Let's talk about constructors.
Author: HolyFire
If you do not know the constructor, take a look at "---- constructor and" first. I will not talk about it any more and go directly to the topic.
When defining an instance of a class, you can see this form
ClassA a; // no parameters are required for the constructor.
Constructors without parameters are called Default constructors.
There are two situations where no parameters are required.
1: The constructor has no parameters.
2: The constructor has parameters but does not provide them.
Class {
Public:
A (); // The constructor has no parameters.
A (int I = 10); // The constructor parameter has A default value.
};
Both cases are Default constructors, but the compiler cannot determine which one to call because of the special nature of the default constructor (which is automatically called, therefore, only one constructor can be created by default.
The appearance of the default constructor means that a type can be created without constraints, just like some small units, proton, neutron and electron. They have great similarity, you do not need to use conditions to identify the information they have been created. Of course, there is no need to use conditions to identify the information they have been created. The second case also exists. Many products of the same variety produced on the assembly line use the same method, then, the information created for them is basically the same, that is, the second condition is met. The default value can be used for parameters.
In this example, we can give an example. When we create a pointer class, we usually set the content to be pointed to as null, which is easy to understand. We need a pointer, but we still don't know who to point to. When we want to use it, we don't necessarily know whether it has pointed to another object. to simplify the problem, we leave it blank at the beginning, however, sometimes we need to use a parameter to specify the object to which it points during creation, especially when a temporary object is generated. Therefore, we use a default constructor with the default value of the parameter being null.
ClassA a (a1); // The constructor has parameters of the same type.
This constructor is called a copy constructor, which means to copy the content of a class instance to a newly created instance. Why. Let's take a look.
When using basic types, you can use the value assignment statement to assign the content of an object of the same type to another object.
Int a = 3;
Int B;
B = a; // in this case, B has the same content as.

Related Article

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.