C + + Replication control: assignment operators and destructors

Source: Internet
Author: User

One, assignment operator

Class defines what happens when an object of that type is assigned a value. As with copy constructors, if the class does not define its own assignment operator, the compiler synthesizes one.

1 . Simple introduction of overloaded operators

The overloaded operator is a function whose name is operator followed by the symbol of the operator defined, and by defining a function named Operator=, we can define the assignment. The formal parameter list of an operator function must have the same parameter as the number of operands (if the operator is a member, the implicit this parameter is included). The assignment is a two-tuple operator, the corresponding two parameters, the first parameter is the left operand, and the second parameter is the right operand.

Attention:

(1) when the operator is a member function, its first operand is implicitly bound to the this pointer.

(2) some operators, such as assignment operators, must be defined as member functions, so the assignment operator can accept a single parameter.

(3) The assignment operator returns a reference to the right-hand operand.

2. Synthetic assignment operator

The synthetic assignment operator performs a member-by-value assignment: Each member of the right operand object is assigned the corresponding member of the left operand object. For arrays, assign values to each array element.

sales_item& Sales_item::sales_item (const Sales_item &rhs) {    = rhs.isbn;     = rhs.units_sold;     = rhs.revenue;     return *this;                // returns a reference to the left operand }

3, copy and assignment often used together

Classes that can use copy constructors are often also able to use synthetic assignment operators. In general, if a class requires a copy constructor, it will also require an assignment operator. Both should be seen as a unit, and if one is needed, we almost certainly need another.

Second, the destructor function

The function of the destructor is to complete the recycling of the required resources as a complement to the class constructors.

1. When to call destructors

Automatically call destructors when undoing class objects:

(1) the variable is automatically revoked when it goes out of scope. For example, the variable item encounters the right}.

(2) dynamically allocated objects are revoked only if the pointer to the object is deleted. For example: pointer p.

New Sales_item; {    Sales_item item (*p);    Delete p;}

Note: destructors are not run when the object's reference or pointer goes out of scope. A destructor is run only if a pointer to a dynamically allocated object is deleted or the actual object (not a reference to the object) goes out of scope.

(3) undoing a container (whether it is a standard library or a built-in array) also runs the destructor for the elements in the container.

The elements in the container are always reversed in reverse order, first undoing the element labeled Size ()-1, and finally undoing the element labeled 0.

{    new sales_item[];    Vectorten);    delete [] p;}

2. when to write an explicit destructor

Many classes do not require explicit destructors, and classes that have constructors do not necessarily need to define their own destructors, only if 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.

Attention:

(1) If a class requires a destructor, it also requires an assignment operator and a copy constructor, which is a useful rule of thumb.

(2) destructors are not limited to releasing resources. In general, destructors can perform arbitrary actions that the Class designer wants to perform after the class object has been used.

3, the synthesis of the destructor function

Unlike copy constructors and assignment operators, compilers always synthesize a destructor for us . The composition destructor revokes each non-static member in reverse order when the object is created, revoking the member in reverse order as the member declares in the class. For each member of a class type, the composition destructor invokes the member's destructor to undo the object.

Note: undoing a built-in type member or a composite type member has no effect, and the composition destructor does not remove the object that the pointer member makes to point to.

4. writing Destructors

The destructor's name is pre-added ~, there is no return value, no formal parameter (so the destructor cannot be overloaded).

Attention:

(1) A class can define multiple constructors, but only one destructor is available to all objects of the class.

(2) The destructor differs from the copy constructor and the assignment operator, even if its own destructor is defined, and the composition destructor is still running.

class sales_item{public:    ~Sales_item () {}private:     string ISBN;     int units_sold;     Double revenue;};

When undoing a Sales_item object, first run the destructor that does nothing, and then run the composition destructor to revoke the members of the class.

C + + Replication control: assignment operators and destructors

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.