C + + constructor essentials

Source: Internet
Author: User

1, Function: The constructor is mainly used to complete the initialization of the member work

2, writing: the function with the same name as the class, generally public , in the case of a singleton can be defined as private, the constructor can take parameters , can also be overloaded , but there is no return type , Constructors may not be supplied manually.

3. Inheritance:

(1) When a base class does not have a constructor defined:

Derived classes can also not define constructors, all with the default constructors.

If only the derived class defines the constructor, you only need to construct the derived class object, and the base class part of the object is automatically created using the default constructor.

(2) A constructor exists in the base class:

A constructor with no parameters exists in the base class. If a base class defines a constructor, and the constructor has no arguments, it can be implicitly inherited, that is, the derived class does not need constructors at all.

A constructor with parameters exists in the base class. If the base class defines a constructor with parameters, the constructor must be inherited and implemented in the derived class. It also provides a way to pass parameters to the base class constructor so that the required data can be obtained when the base class is initialized, using the base keyword when implementing the constructor. After you create an object of a derived class, the program first runs the contents of the base class's constructor, and then the contents of the derived class.

4. copy constructor:

Special constructor , the name of the function must be the same as the class name, which One parameter that must be a reference variable of this type A copy constructor is a constructor that exists on the class itself.

Shallow copy deep copy : The default copy constructor has been able to handle most of the problem, the principle is that the corresponding variable is assigned, but there is a problem, when there are pointers or dynamic members in the variable, the copy pointer may not be the result we want, This is a shallow copy. in the case of a "deep copy", for dynamic members in an object, you cannot simply assign a value, but you should re- provision the space dynamically.

C + + constructor essentials

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.