Points and causes of constructor generation

Source: Internet
Author: User

I believe that many people do not have a thorough understanding of the occurrence and causes of constructor. What's more, they think that default constructor and copy constructor will certainly generate, the member variables should be initialized in the initialization parameter list. Of course, these are for beginners. I will share my views below.

The constructor is not responsible for allocating memory,Only assign values in the allocated memory.. This can be easily seen from the difference between new/delete and malloc/free. malloc/free is only responsible for memory allocation and not initialization, while new/delete is not only responsible for memory allocation, if an object has enough functions, the corresponding constructor will be called. If the object does not exist, the constructor will not be called, for example, int * I = new int [10]; The int type has no constructor, therefore, new is only responsible for allocating 40 bytes of memory and assigning the first address to I. There are no more operations, while string * s = new string (); not only need to allocate the memory required by a string instance, but also call the default constructor of string. To say so much, I just want to say that the constructor is not responsible for memory allocation, but for initialization, that is to say, if you do not want to initialize an instance, how to initialize it is your programmer's business. It has nothing to do with the compiler. the compiler will synthesize the corresponding constructor only when the constructor is required, it will not be merged when it is not needed.

Let's first look at a simple class:

 

 

Run the following simple code:

*p=<<p->x<<endl;== 

There is no problem with row 3. x is a random number, and an error is reported in Row 3. If x is not initialized, the compiler does not automatically synthesize the default constructor. If the random number is not initialized, isn't it silly for the compiler to initialize a random number for you? Look at the source code of the random function. You know how hard people are trying to help you randomly, people quietly help you do a lot of things. It only means that the compiler does not help you initialize. Initialization is a programmer. At least C ++ is like this, and the C # compiler will help you initialize it.

Row 9th does not generate copy constructor, because this class is simple and there is no need to synthesize the copy constructor. the compiler can directly: Memcpy (& p2, & p1, sizeof (Point); doesn't it mean to directly copy the content of an address to another address. It doesn't use a function, the constructor must assign a value to a field, which is slow. In the case that the object is very simple, memset is used for initialization. memcpy is faster than assigning values to fields one by one. With so much nonsense, how can a function be generated?

In a word: if the problem cannot be solved by the bit-by-bit copy, the constructor must be synthesized.. In fact, you can get rid of it (as long as you have a deep understanding of this sentence), but I still have a lot to say to you.

Default constructor needs to be generatedStatus:

1: member variables contain default constructor

2: the parent class contains the default constructor.

3: includes virtual functions

4: virtual inheritance exists in the inheritance relationship

In fact, the four points are summarized as two points: the default constructor needs to be executed and there is a pointer to the method table in the instance. In these four cases, it is not enough to allocate only the required memory. The first two cases need to execute the corresponding default constructor, the corresponding code is of course placed in the default constructor of the current object. In the last two cases, the pointer to the method table needs to be initialized. This must be initialized, it is the responsibility of the compiler, and whether it is the responsibility of the programmer when the field is initialized. Except for these four situations, the constructor does not need to be merged by default. As I have said, constructor is not responsible for allocating memory and compiler is not responsible for initialization, the compiler won't do anything if it's okay. If a programmer adds relevant constructor to multiple tasks, it's absolutely cool. It's hard for your code to pass through the compiler.

 

The copy constructor is also merged when necessary, and will not be merged when not needed. In other words, if the problem cannot be solved by one-bit copy, it will synthesize the copy constructor and do something within its power.

Where the replication constructor needs to be generated:

1: member variables contain copy constructor

2: the parent class contains copy constructor.

3: includes virtual functions

4: The inheritance relationship contains virtual inheritance.

It seems that the cause is similar to the default constructor. You still need to call the corresponding constructor and assign a value to the pointer to the method table. For example, if you assign a subclass object to a parent class object, you need to modify the orientation of the method table, because it may be different from that of the parent class, for more reasons, see how the call of the virtual method is implemented (single inheritance VS multiple inheritance)

The constructor will not be able to generate any problems. Don't say that the six functions will be generated only when necessary, another thing I can't stand is the list of initialization parameters. The Crazy superstition of initializing the parameter list leads to a long list of initialization parameters. Of course, the initialization parameter list does not respond to performance, some member variables can only be initialized in the initialization parameter list, but some member variables have the same initialization performance in the initialization parameter list and in the constructor. Why is the initialization parameter list so long? It hurts.

If a member variable does not have a default constructor, and you do not need to synthesize the default constructor,If the parameter list can no longer be initialized, the initialization in the initialization parameter list is the same as that in the constructor.The reason is very simple. constructor will not synthesize more code for such member variables. The Initialization is the same everywhere. However, if the member variables have relevant constructor, the compiler will help you call them, the code in the initialization parameter list will be placed in the constructor. If the variable is initialized in the initialization parameter list, it will only be initialized once. If the variable is initialized in the function, the compiler does not check that you have not initialized it in the initialization parameter list. If you haven't initialized it, code will be generated to help you initialize it. If you initialize it in the constructor, it will be initialized twice, this is why many beginners think that the initialization parameter list has a high performance.

 

To sum up, when a bit-by-bit copy fails to solve the problem, the compiler will synthesize the relevant constructor and execute the relevant code. during initialization, it is clear that you have not initialized the constructor, there are related constructors, so they help you call them. In other cases, the compiler is not responsible, which is what we programmers do.

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.