C++6 a default member function

Source: Internet
Author: User
Tags shallow copy

Question: What class member functions are generated by default for empty classes in C + +?

Default constructors and copy constructors (copy constructors) for the system

A destructor provided by default by the system.

System default operator overloading function (copy assignment function): Used to assign values between homogeneous objects.

System default value operation: Called when the object of the class is taken to the address (&).


1. The necessity and effect of the existence of the constructor method and the destructor:

(1). The necessity and function of constructors (to ensure initialization with constructors):


      1. Resolves initialization problems for different objects of the same type.

      2. Ensure that the data members of each object have appropriate initial values.

(2). The necessity and function of the destructor (use destructors to ensure cleanup):


      1. Reclaims memory and resources, typically used to release resources acquired during the lifetime of a constructor or object.

      2. The compiler calls the destructor automatically when the object exceeds its defined scope.


2. Types of constructors and destructors:

(1). A class can have more than one constructor (that is, the overload of the constructor).

A. Default constructor: A constructor with no arguments.

The default constructor is important when the compiler needs to create an object without knowing any details. When there is no constructor in a constructed type, the compiler automatically creates one, however, once there are other constructors there is no default constructor.


B. Overloaded constructors: constructors with parameters.

Overloads that belong to a class member function form an overloaded condition by the number, type, and order of the parameters. The special cases are:

If there are other constructors in the class, the system does not provide a default constructor to construct the object.

If the constructor in the class has default parameters, it prevents overloading ambiguity and an error occurs. When a constructor in a class forms an overload, care is taken to prevent an overloaded ambiguity from overloading an implicit type conversion in an overloaded function.

C. Copy constructor (copy constructor):

The system provides a default copy constructor (that is, a shallow copy or a full copy), as well as a custom copy constructor (that is, a deep copy or a partial copy).


(2). A class has only one destructor (that is, the destructor does not have overloads),

Destructors are also categorized as system default and custom destructors (complete other functions specified).


3. Specific knowledge points about copy constructors (copy constructors):

(1). Define the format:

Class name (class name +& object)

{

}

Features: No return value, parameter only and can only be a reference to a homogeneous object.


Troubleshoot: Why is it not allowed to define this form of copy constructor for a (a copy)?

The copy constructor calls the copy constructor itself during parameter passing, and the copy constructor is called for parameter passing and the copy constructor is called ... So stuck in the endless allocation stack of infinite recursion, and each time the compression stack is nested in the stack, so that every time the stack can not be completed, so the compiler can not pass this form of copy constructor.


(2). Comparison of Copy constructors and assignment operator overloads:

Different places:

A. A copy constructor is called when an object is created and initialized with another object that already exists ;

such as String S1 = "Hello World";

string s2 = S1; Equivalent to String s2 (S1);


B. The copy assignment function can only assign one object to another object that already exists, so that the existing object has the same state as the source object .

such as String S3;

s3 = S1;


The same place:

The default copy constructor and default assignment operator overloads are implemented by using the copy-by-member default method.

Take two objects A and B of the class string as an example. Suppose the content of a.m_data is "Hello", the content of B.m_data is "world". Now assigns A to B, and the default assignment operator overload, copy by member, means that B.m_data=a.m_data is executed. This will cause three errors :

(1) B.m_data memory is not released, causing a memory leak.

(2) B.m_data and A.m_data point to the same piece of memory area, a or b change will affect the other party

(3) M_data was deleted two times while the object was being refactored.


4. Constructor initialization list Knowledge points:

1). The construction order of the initialization list:

The order in which the data members in the initialization list is constructed is in accordance with the order in which they are declared. Instead, it is related to the order in which they appear in the initialization list.


2.) must the initialization list be used ?

Reference (const) members and members of the reference type can only be initialized and cannot be assigned outside the initialization list.

The constructor of the base class in the inheritance of the class must be called by the initialization list (call the base class constructor first, then the constructor of itself).

The construction of a nested object of an aggregate class in an aggregation of a class can be placed in its constructor body for assignment without necessarily initializing it, consistent with normal member variables.


This article is from the "sunshine225" blog, make sure to keep this source http://10707460.blog.51cto.com/10697460/1826492

C++6 a default member function

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.