C + + Primer Chinese version of Learning notes (12) __c++

Source: Internet
Author: User
Tags class definition shallow copy
13th Chapter Copy Control 1 Copy constructors, assignment operators, and destructors are always referred to as replication control. 21 common scenarios require classes to define their own replication control members: The class has pointer members. (deep copy, shallow copy problem) Rule 33: If a class requires a destructor, it also requires an assignment operator and a copy constructor.

Exception: If the base class has an empty destructor in order to set the destructor as a virtual function, then the class has a destructor that does not represent the need for assignment operators and copy constructors. The 4         copy constructor is used to: a)          explicitly or implicitly initializes an object based on another object of the same type
B           copy an object and pass it as an argument to a function
C)           Copy an Object
D)          initialize the elements in a sequential container
from the function (e)           initializes the array element
based on the element initialization list 5        c++ Supports two initialization forms a)          direct initialization (directly invokes the constructor that matches the arguments) b)          copy initialization (always call the copy constructor for compatibility with C)

Step: First create a temporary object using the specified constructor, and then copy the temporary object to the object that the car is creating with the copy constructor. 6 You must define a copy constructor: (A class that contains a class type or a built-in type (not a pointer) member without having to explicitly define a copy constructor or copy) a The data member is a pointer or has a member representing other resources allocated in the constructor
b or you must do some specific work when creating new objects
7 to prevent user code from replicating objects of class types, a class must explicitly declare its copy constructor to be private

At this point, friends and members of the class can still replicate, and if you want to prevent replication in friends and members, you can declare a (private) copy constructor but not define it. Or it's a practice to use base class like uncopyable.

Disadvantage: Class objects that are not allowed to replicate can only be passed as references to functions or returned from functions, nor can they be used as elements of a container.

Example:io type Object, the dynamically allocated object of the copy constructor to private 8 is revoked only if the pointer to the object is deleted. If you do not delete a pointer to a dynamic object, the destructor of the object is not run, and the object persists, causing a memory leak, and any resources used within the object will not be freed.

The destructor is run only if you delete a pointer to a dynamically assigned object or an actual object (not a reference to an object) beyond the scope. 9         most C + + classes manage pointer members using one of the following three methods: a)          Pointer members take conventional pointer behavior. Such a class has all the flaws of the pointer, but no special replication control is required
B)          class can implement what is called "smart pointer" behavior. The object to which the pointer points is shared, but the class is able to prevent the dangling pointer
C)           class from taking a value-type behavior. The object pointed to by the pointer is unique and is managed independently by each class object.
10  creates and runs a composite destructor, regardless of whether the class defines its own destructor. If the class defines a destructor, the composite destructor is run after the destructor of the class definition ends.

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.