What do you know about a basic question that leads to a topic of common sense?

Source: Internet
Author: User

Previously felt that, write the article is a waste of time, there is so little time than play games, and girls chat. Now it feels that writing reading notes is also good, one for information sharing, (each cloud platform is not to share, then big Data) and then the new, deepen the understanding of knowledge. Don't say much, keep writing.

question: How many functions will he produce in an empty class in C + +?

The answer is still to be revealed, first introduce C + + several member functions.

constructor Function:

What is a constructor function? What is the function of the constructor? How many constructors can I have?

First, the constructor is a special member function in the class that has the same name as the class and no return value. What is the function of the constructor? Personally, I think he was primarily used to provide various initializations of classes.

I've just talked about various initializations, and presumably everyone knows that constructors can be multiple.

In the absence of a defined constructor, the compiler automatically generates a constructor that is free of parameters, and 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 A;

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 a shape parameter, which means that if we want to new, we can only pass an int parameter. If you do not pass the parameter, you will get an error, which indirectly indicates that defining the constructor will not be the default constructor for the parameter.

So, if we need a parameterless constructor, we have to show the definition. In general, defining a constructor with parameters is to define a parameterless constructor, which is primarily considered in terms of security and convenience. Because we are often a x=new a (); If no parameterless constructor is defined, we may not know where the error is.

The main function of the constructor is to initialize the member data. An initialization method is provided above, and there is a class a:b (5) {};

It is important to note that the order in which C + + member variables are initialized is consistent in the order in which they are declared. Regardless of the initialization order of the constructor.

A special constructor: copy Constructor

Note that this is a copy, not an assignment.

The copy constructor is actually the copy process of the object. Examples are as follows.

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

There is also a value assignment. Below that is. where x is assigned to M.

1 A x (6), m=x;

When it comes to copy constructors, it is important to emphasize the concept of deep copy and shallow copy . Also known as deep copy, shallow copy .

First clarify my two concepts here, if object A is copied to B, then a is the original object and B is the new object.

Shallow copy means that all variables of the new object contain the value of the original object, but the reference still points to the original object.

Deep copy means that all variables of the new object contain the value of the original object, and all references are copied.

How does that make sense? In fact, after the shallow copy, the reference is still on the original object (so once the original object pointer is released, the new object's pointer will not know where to point, the wild Child is the case). And the deep copy, the reference also copied (create their own resources, the original object again toss also affect me). See below for specific examples.

1 struct test{2     char *ptr; 3}; 4 void shallow_copy (Test &dest,const test &source) {//Shallow copy 5     DEST.PTR=SOURC E.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}

Shallow copy can easily cause a program to crash, such as the original object has a dynamic pointer, if the shallow copy, then the new object is pointing to the original object pointer, if the original object is released. So where does the new object point??

For more information on this, please refer to the relevant article.

Destructors

In contrast to constructors, which are constructors that initialize object members, the destructor is the release of resources, and the general destructor is executed at the time of the last exit.

Define words like constructors, add a ~ to the constructor, such as ~a () {};

In addition, the destructor has no parameters, no return value, and only one destructor.

In inheritance, such as the Son class inherits the parent class, son Littleson; This procedure invokes the constructor of the parent, and then calls the son class's constructor, and the destructor is a reverse procedure that calls the destructor of the subclass and then calls the destructor of the parent class. In fact, we can easily understand this from the perspective of resource allocation and recycling.

When it comes to allocating resources, we know that the compiler is automatically assigned to do it in the stack, and the programmer requests the resources and frees the resources in the heap.

There are two different ways to apply resources in C + +, and there are two ways toNew,mallocthe release of the Delete,free. Let's analyze the similarities and differences between the two approaches.

The same we will not say, all can carry on the dynamic application of resources is their greatest similarities, then different?

1. malloc is the calling function, and new is the operator

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

3. malloc is unsafe, and new is secure (no compile errors when applying) such as: int p= (int) malloc (sizeof (double));

4, Maolloc need library file support, function definitely need ah, and new don't need, because people are operators Ah, you think + must have library file to know.

5, new calls the constructor, and delete calls the destructor.

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

malloc is an old-fashioned resource application model, and it is no doubt that new applications are now advocated. But there is a situation with malloc will get a better effect, that is, when the program after a application, may be used to complete the need to apply again, because there is realloc ah. Of course, you can also use vectors.

Now you can answer the first question, it will produce the member functions at least include: constructors, destructors, copy constructors, fetch operator overloaded functions, assignment operator overloading functions, const accessor operator overloaded functions.

What do you know about a basic question that leads to a topic of common sense?

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.