Analysis class default constructor, copy constructor, assign value copy function "="

Source: Internet
Author: User

Each class has only one destructor, but multiple Constructors (including one default constructor, one copy constructor, and other common constructor) multiple assignment functions (including one copy assignment function, and others are normal assignment functions ). Generally, for any class A, if the programmer does not declare and define the above functions, the C ++ compiler will automatically generate four default public inline functions for Class, the most common form of these four functions is:

A () // default constructor

A (const A &) // default copy constructor

~ A () // default destructor

A & operator = (const A &) // default value assignment function.

Let's use a program to verify the existence of these four functions.

# Include <iostream>

Using namespace STD;

Class
{
};

Int main ()
{
A;
Cout <"OK! Constructor with no parameters by default "<Endl;
A B ();
Cout <"OK! The default copy constructor "<Endl;
B =;
Cout <"OK! There is a default copy assignment function "<Endl;

Return 0;
}

After VC compilation is successful and runs successfully, it indicates that a class contains default non-parameter constructor, default copy constructor, and default value assignment function.

 

So we should exercise our own rights and rewrite these functions according to our own ideas!

 

Next I want to explain how these functions are called when constructing objects?

We also use an example to describe.

 

# Include <iostream>

Using namespace STD;

Class
{
Public:
A ()
{
Cout <"A: ()! "<Endl;
};
A (const A &)
{
Cout <"A: A (const A & )! "<Endl;
}

A & operator = (const A &)
{
Cout <"A: A & operator = (const A & )! "<Endl;
Return * this;
}
};

Int main ()
{
Cout <"call a constructor without Parameters" <Endl;
A A1;
Cout <Endl;
 
Cout <"Call copy constructor" <Endl;
A A2 (A1 );
Cout <Endl;
 

Cout <"Call copy constructor" <Endl;
A A3 = A1;
Cout <Endl;

Cout <"Call the copy assignment function" <Endl;
A2 = A1;
Cout <Endl;

Return 0;
}

 

Through this example, we can know that there are three ways to create an object,

1. Direct definition, such as a A1;

2. Define copies through other objects, such as a A2 (A1 );

3. Use "=" to assign values, for example, a A3 = A1;

There are three different methods to call a function.

The first method is to call a common constructor to initialize an object.

The second method is to call the copy constructor to initialize the object.

The third method is to call the copy constructor to initialize the object.

Note that the second and third Methods call the same function, especially the third method, and do not call the copy assignment function.

 

Program running

If we add a function in Class A, bool hasacceptablequality (A), we use a3.hasacceptablequality (A1) when calling it)

In this function, we use the value transfer method, so when calling this function, the real parameter A1 will pass the value to the form parameter A by copying the constructor; so when we use objects as parameters,

Make sure to use reference transfer or pointer transfer as much as possible to improve efficiency!

The following is a program instance.

# Include <iostream>

Using namespace STD;

Class
{
Public:
A ()
{
Cout <"A: ()! "<Endl;
};
A (const A &)
{
Cout <"A: A (const A & )! "<Endl;
}
 
A & operator = (const A &)
{
Cout <"A: A & operator = (const A & )! "<Endl;
Return * this;
}

Bool hasacceptablequality ()
{
Cout <"hasacceptalbequality! "<Endl;
Return true;
}

};

Int main ()
{
Cout <"call a constructor without Parameters" <Endl;
A A1;
Cout <Endl;
 
Cout <"Call copy constructor" <Endl;
A A2 (A1 );
Cout <Endl;
 
Cout <"Call the copy assignment function" <Endl;
A2 = A1;
Cout <Endl;
 
Cout <"Call copy constructor" <Endl;
A A3 = A1;
Cout <Endl;

A3.hasacceptablequality (A1 );

 
Return 0;
}

Result chart

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.