What do you know about the common sense of a basic question ?, Topic knowledge

Source: Internet
Author: User

What do you know about the common sense of a basic question ?, Topic knowledge

I used to think that writing articles is a waste of time. It is better to play games and chat with girls at that time. Now, I think it is good to write and write Reading Notes, and share information (not just share and then use big data on various cloud platforms) to learn new things and deepen my understanding of knowledge. Continue writing.

Problem:In C ++, how many functions does a blank class generate?

The answer is to reveal it slowly. First, we will introduce several member functions in C ++.

Constructor:

What is a constructor? What is the role of Constructors? How many constructors can be created?

First, constructor is a special member function in a class. It has the same name as the class and has no return value. What is the role of Constructors? I personally think it is mainly used to provide various class initialization.

I 've just talked about various initialization methods, so we'll surely know that there can be multiple constructors.

When no constructor is displayed, the compiler automatically generates a constructor without parameters. Of course, if we have defined several or one constructor in the class, the compiler will not generate a constructor. For example, there is A Class;

1 class A{2 public:3      A(int a){4         b=a;5         cout<<b;6     }7 private:8     int b;9 };

In this class, I define a constructor with an integer parameter, which means that if we want to create a New object, we can only pass one int parameter. If no parameter is passed, an error is returned, which indirectly indicates that if the constructor is defined, the default constructor is not used.

Therefore, if we need a constructor without parameters, we must display the definition. Generally, defining a constructor with parameters requires defining a constructor without parameters, which is mainly considered from the security and convenience perspectives. Because we often use A x = new A (). If no parameter-free constructor is defined, we may not know where the error is.

The main function of the constructor is to initialize the member data. The above provides an initialization method, and another initialization method is class A: B (5 ){};

Note that the initialization sequence of the C ++ member variables is consistent with the declared sequence. The initialization order of the constructor is irrelevant.

 

A special constructor:Copy constructor

Note: Here is copying, rather than assigning values.

The copy constructor is actually the object copy process. Example.

1      A(const A &other){2          this->b=other.b;3          cout<<this->b;4      }

Another method is assign values. Below is. X is assigned to m.

1 A x(6),m=x;

When talking about the replication constructor, it is necessary to emphasizeDeep replication and light Replication. Also knownDeep copy and light copy.

First, clarify the two concepts here. If object A is copied to object B, it is called A as the original object and B as the new object.

Shortest means that all the variables of the new object contain the values of the original object, but the reference still points to the original object.

Deep replication means that all the variables of the new object contain the values of the original object, and all references are also copied.

How can this be understood? In fact, after the shortest copy, the reference is still for the original object (so once the original object pointer is released, the pointer of the new object does not know where to point, and this is how the wild child appears ). Deep replication also copies the references. (If you have created your own resources, the original object will not affect me any more ). For specific examples, see the following.

1 struct Test {2 char * ptr; 3}; 4 void shallow_copy (Test & dest, const Test & source) {// copy 5 dest. ptr = source. ptr; 6} 7 void deep_copy (Test & dest, const Test & source) {// deep copy 8 dest. ptr = (char *) malloc (strlen (source. ptr) + 1); 9 memcpy (dest. ptr, source. ptr, strlen (source. ptr) + 1); 10}

A shortest copy can easily cause program crash. For example, if the original object has a dynamic pointer, the new object points to the pointer of the original object if the original object is released. so where does the new object point ??

For more information, see related articles.

Destructor

The constructor is opposite to the constructor. If the constructor is the initialization object member, the constructor releases the resource. Generally, all the constructor is executed at the final exit.

The definition is similar to the constructor. Add a ~ to the constructor ~, For example ~ A (){};

In addition, the Destructor has no parameters, no return value, and only one destructor can be created.

In inheritance, for example, the son class inherits the parent class, son littleson. In this process, the constructor of the parent class is called first, and then the son class constructor is called; the Destructor is the opposite process. First, the destructor of the subclass is called, and then the destructor of the parent class is called. In fact, this is easy to understand from the perspective of resource allocation and recovery.

When talking about resource allocation, we know that automatic compiler allocation is carried out in the stack, and programmers apply for and release resources in the stack.

There are two different methods to apply for resources in C ++,New, mallocThere are two ways to delete and free. Let's analyze the similarities and differences between the two methods.

We will not say much about the same. It is their greatest difference that we can apply for resources dynamically. What is the difference?

1. malloc is a call function, while new is an operator.

2. malloc needs to calculate the number of bytes requested and convert it to the corresponding type. new is automatically calculated.

3. malloc is insecure, while New is secure (No compilation error occurs during application) such as: int p = (int) malloc (sizeof (double ));

4. maolloc requires the support of library files. functions are certainly needed, but new is not. Because they are operators, you can see that + requires a library file.

5. new calls constructor and delete calls destructor.

So it seems that new is more than malloc, so we don't need malloc?

Malloc is an old type of resource application model. It is undoubtedly recommended to apply for a new resource. However, there is a situation where using malloc will achieve better results, that is, when the application is completed after an application, it may need to be applied again because of realloc. Of course, vector can also be used.

Now you can answer the first question. The member functions it generates include at least constructor, destructor, copy constructor, address-taking operator overload function, and assign value operator overload function, the const bitwise operator reloads the function.

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.