C + + supplements (vi)--Replication control

Source: Internet
Author: User

Busy a few days ago, until now is a free rest. I wish you a Happy New year, I will smile o.o

This blog post mainly describes the definition of a type of object when the replication control mode, this part of the content before a certain understanding but a few, and always feel that the point is not able to find. Years ago picked up the book carefully read, is a little new understanding. I wanted to write a few days ago, and I haven't had the time to drag it to the present.

Each type defines what happens when an object of that type is created-the constructor defines the initialization of the object of that class type. Types can also control what happens when an object of that type is copied, assigned, or revoked-the class controls these behaviors through special member functions: Copy constructors, assignment operators, and destructors.

A copy constructor is a special constructor that has a single formal parameter, which is a reference to the class type, which is a common const adornment. When you define a new object and initialize it with an object of the same type, the copy constructor is used explicitly. When you pass an object of that type to a function or function that returns an object of that type, the copy constructor is implicitly used. Assignment operators can be overloaded by specifying the right operand of different types. The right operand is a special version of the class type: If we do not write this version, the compiler will synthesize one for us. Destructors are complementary to constructors: destructors are automatically applied when objects that are out of scope or dynamically allocated are deleted. Destructors can be used to dispose of resources when objects are constructed or acquired during the life of an object. The compiler automatically executes destructors for non-static data members of the class, regardless of whether the class defines its own destructor. The copy constructor, assignment operator, and Destructor are always referred to as replication controls. The compiler automatically implements these operations, but the class can also define its own version.

Copy constructor

    • Only a single parameter, which is a reference to objects of this class type (commonly used const adornments), is called a copy constructor. Like the default constructor, a copy constructor can be called implicitly by the compiler.
    • If we do not define a copy constructor, the compiler will synthesize one for us. Unlike the composition's default constructor, a copy constructor is synthesized even if we define other constructors. The behavior of the composite copy constructor is to perform a member-by-instance initialization to initialize the new object to a copy of the original object.
    • In general, the most difficult part of defining a copy constructor is recognizing the need to copy the constructor. As long as you realize that you need to copy a constructor, it is generally very simple to define a constructor function. The definition of a copy constructor is the same as the other constructors: it has the same name as the class, there is no return value, you can (and should) initialize the members of the newly created object using the constructor initialization list, and you can do any other necessary work in the body of the function.
    • Some classes require a complete ban on replication. For example, the Iostream class would not allow replication. If you want to suppress replication, it seems that you can omit the copy constructor, however, if you do not define a copy constructor, the compiler will synthesize one. To prevent replication, the class must explicitly declare its copy constructor as private. However, friends and members of the class can still replicate. If you want to disallow replication in friends and members, you can declare a (private) copy constructor but not define it. Declaring a member function without defining it is legal, but any attempt to use an undefined member will cause the link to fail. By declaring (but not defining) the private copy constructor, you can suppress any attempt to replicate a class type object: The copy attempt in user code is marked as error at compile time, and the copy attempt in the member function and friend causes an error at link time.

Assignment operator

    • The overloaded operator is a function whose name is operator followed by the symbol of the operator that is defined. Therefore, by defining a function named Operator=, we can define the assignment. Like any other function, the operator function has a return value and a formal parameter list. The formal parameter list must have the same parameters as the number of operators (if the operator is a class member, the implicit this parameter is included.) The assignment is a two-tuple operation, so the operator function has two parameters: the first parameter corresponds to the left operand, and the second parameter corresponds to the right operand. Most operators can be defined as member functions or non-member functions. When the operator is a member function, its first operand is implicitly bound to the this pointer. Some operators, including assignment operators, must be members of the class that defines them. Because the assignment must be a member of the class, this is bound to a pointer to the left operand. Therefore, the assignment operator accepts a single parameter, and the parameter is an object of the same class type. The right operand is generally passed as a const reference. The return type of the assignment operator should be the same as the type returned by the built-in type assignment operation. The assignment operation of the built-in type returns a reference to the right operand, so the assignment operator also returns a reference to the same class type.

Destructors

    • One use of constructors is to obtain resources automatically. For example, a constructor can allocate a buffer or open a file, and after a resource is allocated in the constructor, a corresponding action is required to automatically reclaim or dispose of the resource. A destructor is a special function that can complete the required resource collection as a complement to the class constructor.
    • Many classes do not require explicit destructors, especially classes that have constructors that do not necessarily need to define their own destructors. Destructors are only required when some work requires a destructor to complete. Destructors are typically used to release resources that are acquired in a constructor or during the lifetime of an object. If a class requires a destructor, it also requires an assignment operator and a copy constructor, which is a useful rule of thumb. This rule is often called a three-rule, meaning that if a destructor is required, all three copy control members are required. Unlike a copy constructor or assignment operator, the compiler always synthesizes a destructor for us. The composition destructor revokes each non-static member in reverse order when the object is created, so that it revokes the member in the reverse sequence of declaring the member in the class. For each member of a class type, the composition destructor calls the member's destructor to undo the object.
    • A destructor is a member function whose name is preceded by the class name with a tilde (~), which has no return value and no formal parameters. Destructors cannot be overloaded because no formal parameters can be specified. Although you can define more than one constructor for a class, only one destructor is available that applies to all objects of the class. An important difference between a destructor and a copy constructor or assignment operator is that, even if we write our own destructor, the composition destructor is still running.

Managing pointer members

Different replication control policies enable you to implement different behaviors for pointer members. One of these is smart pointers. In this section, I intend to elaborate on the next blog dedicated to pointers (if a careful reader has read this series of blogs before, it should be noted that I have not mentioned the unavoidable nature of pointers to the present). The content of this article is not complete, there is a message processing instance did not show, there is time to update, now a bit tired haha.

Finally, I wish you a happy New Year!

C + + supplements (vi)--Replication control

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.