Deep Exploration C + + object Model (4)

Source: Internet
Author: User
Tags constructor object model

Thor stumbled on the first chapter of the Deep Exploration of C + + object model, although some doubts, but already feel a lot of harvest. According to friends, the first chapter is a general introduction, the specific details will be elaborated in later chapters, if not read through the book, the first chapter is relatively difficult to understand. Thor had heard it before, and he didn't feel the end of the world when he first read it (in the 2nd article, Thor felt that he had learned nearly a year of C + +, the level is so poor, and through their own efforts, or touch some doorway, so let us continue to happy to embark on a deep exploration of the C + + object model of the journey. Remember we are in the first article of "perseverance, until success," which is the success of the begotten.
The second chapter is mainly about the structural function semantics (semantics), what does this mean? My English and Chinese literature are not good, but I think the book is wrong (perhaps just a clerical error), perhaps should be translated idiom meaning more appropriate. The study or the meaning in anguage forms. A study or science that expresses meaning in linguistic form. We're going to look at the constructor and describe it in terms of language.
Read the title my first feeling, constructor I know. A constructor is a member function of a class, constructors and destructors are member functions that perform object data creation, initialization, and cleanup, and you can overload constructors so that a class has more than one constructor, because sometimes you need to create different objects in one of these methods. Destructors cannot be overloaded. Constructors have the same name as member functions and classes. Example: A class name is: AClass, and the constructor is AClass (). The constructor does not have a return value and cannot define its return type, nor is void. Destructors also use this point. When you write overloaded functions, you can distinguish between two overloaded functions by comparing the number of parameters or parameter types, only if the parameter tables are different. But after I read the first little paragraph, I knew what the chapter was going to tell us.

This chapter is not about telling us what a constructor is and what it does. But to tell us how the constructor works. It's mine. I'm excited to learn this because I really don't know how constructors construct objects of a class, and always want to know. I've always been interested in object-oriented magical features. Why is a class when instantiated, can automate a lot of work, make our main function clear, simple, robust, efficient. Previously only saw the surface, not deep, this will we have the opportunity to peel and pick meat into the bone marrow. The book mainly discusses several situations:

The member object with the default constructor. If a class does not have any constructors, but he has a member object whose class has a default constructor, the compiler will synthesize a constructor for the class when needed.

As an example:

We have the following several classes. They all have a constructor.


Cat {public: Cat (), ...};
Dog {public: Dog (), ...};
Bird {public: Bird (), ...};
Fish {public: fish (), ...};

We have another class. Pet, we will be the cat as one of its members. And it is not declared to be a constructor.

Pet
Public
Cat a cat;
Dog a dog;
Bird a bird;
Fish a fish;
Private
int ival;
......
}

Then the compiler will synthesize a constructor for it when it is needed, and use the inline method. Probably like the following.

Inline
Pets:: Pets ()
{
Cat Cat:: Cat ();
Dogs, Dogs:: Dogs ();
Bird Bird:: Bird ();
Fish. Fish:: Fish ();
ival=0;
}

Why, let's take a look at the compiler's actions. Before the compiler starts executing the user's code and prepares to generate the Pet object, it first invokes the necessary constructors to initialize the members of the class to allocate the appropriate memory space for the object. The result compiler will synthesize the above constructor if the programmer writes a constructor for the pet class. Pet:: Pet () {ival=0;} The compiler will also expand the constructor into the above. How does the compiler implement it? When a class does not have any user-defined constructors, but is automatically generated by the compiler, the implicitly generated constructor will be a useless constructor. But working with the compiler can synthesize a nontrivial default constructor for us.

Like Hong Kong movies, if you get sued (you have to design a class), you don't have the money to ask a senior lawyer (no constructor), that will give you a lawyer (the default constructor), and of course the lawyer's ability may be different from those of the barrister (trivial). But we have to know that they are not useless. But with the supervision of law firms, these lawyers can be made to do their best (nontrivial).

In the same way, we can understand other kinds of nontrivial default constructor.

If your class does not have any constructors, and it derives from a base class with a default constructor, the default constructor for this derived class is considered nontrivial, so it needs to be synthesized, and his compositing step is to call the default constructor of the previous-layer base class. and to synthesize a constructor for the derived class based on their declaration order.

If a class declares or inherits a virtual function, or the class derives from a chain of inherited strings, there is one or more virtual base classes. Because of the lack of a user-declared constructor, the compiler synthesizes a default constructor to properly initialize the vptr of each class object.

Finally, in the composite default constructor, only the child objects of the base class and the member objects of the class are initialized, and all other non-static data members are not initialized because these operations require the programmer to do so. There is no need for the compiler to do all this work. All right, let's write this down here. This book is really the slowest one in the book that Thor has ever read. But are these deep knowledge necessary to understand clearly that we do not know how the compiler synthesizes the default constructor can not write programs? Thor used the words of the master to answer this question: practice from the difficulty of practice, use from the easy place. Know it but do not know why, not a rigorous learning attitude.

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.