Replication Control + smart pointer

Source: Internet
Author: User

Today, I have read chapter 13 of C ++ primer and summarized it.

I. Replication Control

1: Replication control is a general term for copying constructor, value assignment operator, and destructor. They have some similarities, so they are collectively referred to as "replication control ".

What are their commonalities?

A: If it is not defined, the system will automatically merge it;

B: When there are pointers in the object members of the class, we usually need to define the three. If you want to define the destructor, you must define the copy constructor and the value assignment operator. This is called the "Three Rules ".

Differences:

If the copy constructor and the value assignment operator are defined by themselves, the system will not be merged, but the destructor system will always be merged. (P-414) using the characteristics of the copy constructor and the value assignment operator, can not be assigned to avoid the assignment. We can just declare that we do not define it, so that we can avoid System Synthesis and achieve our goal without doing anything ourselves.

2: Why use the copy constructor?

When to define the copy constructor and value assignment operator

Generally, when a data member of a class has pointer variables or special operations on an object, you need to define the copy constructor and the value assignment operator. The following table lists the data members of a class with pointer variables.

The procedure is as follows:

# Include <stdio. h >#include <string >#include <iostream> using namespace STD; struct noname {public: noname (STD: string * str_f, int I _f, double d_f ): pstring (str_f), I (I _f), D (d_f) {}; noname (const noname & A) {pstring = new STD: string; * (this-> pstring) = * (. pstring); this-> I =. i; this-> d =. d;} public: STD: string * pstring; int I; double D ;}; int main () {string a (4, 'x'); string B (4, 'y'); string * str_a = & A; // The noname object1 (str_a, 2, 3.0); noname object2 (object1); * (object2.pstring) = B; cout <* (object1.pstring) <Endl; System ("pause"); return 1 ;}

For this class containing pointer members, because only the pointer address is copied during replication, they actually share a base. It is not terrible to change the content of other objects by changing the content. What is terrible is the pointer suspension caused by the deletion.

2. pointer suspension and smart pointer

Pointer suspension is also called a "wild pointer". It is only when the Pointer Points uncertain to the memory indicated by the pointer is deleted. We can actually access this type of pointer, but the obtained value is not necessarily the same every time, because the direction of the pointer is actually not fixed at this time. However, we cannot perform operations on the content, such as modifying or deleting values. That is to say,Yes, but you do not have the operation permission..

There are two methods to solve pointer suspension.

A: Create a copy constructor.

This method performs a copy operation on each pointer, so that by changing the pointer of other objects, nothing else can be affected.

B: use smart pointers

Smart pointers can solve the issue of pointer suspension, but they cannot solve the problem of changing the value of an object and affecting other object values. So what is smart pointer? In C ++ primer (p-423), it is clear that what is a smart pointer and how to construct a smart pointer. The Code is as follows:

class U_Ptr{friend class HasPtr;U_Ptr(int *p):ip(p),use(1){}~U_Ptr(){delete ip;}int *ip;size_t use;};class HasPtr{public:HasPtr(int *p,int i):ptr(new U_Ptr(p)),val(i){}HasPtr(const HasPtr &orig):ptr(orig.ptr),val(orig.val){orig.ptr->use++;}HasPtr& operator=(const HasPtr &rhs);~HasPtr(){if(--ptr->use==0) delete ptr;}private:U_Ptr *ptr;int val;};HasPtr& HasPtr::operator =(const HasPtr &rhs){rhs.ptr->use++;if(--this->ptr->use==0)delete this;this->ptr=rhs.ptr;this->val=rhs.val;return *this;}

Here are some notes:

1: Smart pointers are the bond between objects and units. They are essentially a transitional function;

2: The smart pointer is only used for the hasptr class and we do not want users to use it. Therefore, we define all its data as private and hasptr as its friends;

3: here we add two items that have nothing to do with the above.Two knowledge points:

1): New u_ptr (P) is a new u_ptr object. Its Parameter requirements are closely related to the u_ptr constructor form;

2): The expression of the youyuan class means that the object in the youyuan class can access the private functions in its class, that isMembers of any class can only access their objects.There is no room for discussion.

Iii. Values assignment operators and destructor

The assignment operator is similar to the copy constructor, Which is omitted here. This section describes the destructor.

1: Role of destructor

The Destructor correspond to the constructor, which is used to release space. But because the system will automatically synthesize the destructor, under what circumstances do we need to define the Destructor?

2: When to define the destructor

You need to manually delete "object reference or Object Pointer" and "dynamically applied object". In this case, you generally need to use the destructor. Or we want to perform other operations while deleting the object. This can also be done through the destructor. Other functions can be resolved through 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.