C + + notes (11): Copy Control (copy move, construct assignment, destructor)

Source: Internet
Author: User

Control object Copy, assignment, destructor

Copy constructor, move constructor

Copy assignment operator, move assignment operator

Destructors

--------------------------------------------------------------------------------------------------------------- ----------------------

1. Copy constructor: The parameter must be a reference type &, typically a const

The 1th parameter of a copy constructor refers to a reference to its own class type

2. Copy assignment operator: essentially operator function overloading

3. Destructors: Releasing and destroying objects, no return value

Requires the compiler to generate a composite version of the copy constructor, copy assignment operator, and destructor using "=default"

block copy : 1) define deleted functions: Copy constructor, copy assignment operator using "=delete"

2) destructor cannot be a deleted member

3) Private Copy Control: copy constructor, copy assignment operator using "private"

Object Movement : For objects that are destroyed after some copies, moving, not copying, can significantly improve performance

New standard in order to support the move Operation , a new reference type is introduced: rvalue reference:&&

An rvalue reference can only be bound to an object that is about to be destroyed:int &&r=42; rvalue reference (int &r=i is a generic lvalue reference)

Or use the standard move function Move (): int &&r=move (r1);

C + + notes (11): Copy Control (copy move, construct assignment, destructor)

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.