Default member functions in C + +

Source: Internet
Author: User

In general, for a user-defined class type, the following four functions are automatically generated by the compiler when the user does not have a custom condition:

1. Default constructor

2. Copy constructor

Someclass::someclass (const SomeClass &);

3. Copy assignment operator

SomeClass & someclass::operator= (const SomeClass &);

4, destructor

For C++11, a further two additions were made:

5. Move constructor

Someclass::someclass (SomeClass &&);

6. Move Assignment

SomeClass & Someclass::operator= (SomeClass &&);

To keep in mind the prototypes of these functions (input parameters, return types), there are several rules about when the compiler will not generate the above default member functions:

The compiler will not generate a default constructor if the user customizes any form of the constructor (even if it is a copy constructor)

If a destructor, or copy constructor or copy assignment operator is defined, the compiler will not generate the default move constructor and move assignment

If you define move Construcor or move assignment, the compiler will not generate a default copy constructor or copy assignment operator.

Finally, in the c++11 there is a very chic lazy way,

Someclass::someclass (const SomeClass &) = default, which means that the user needs the compiler to generate the member function by default, which is only valid for the above six functions.

Default member functions in C + +

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.