Explore the C + + object model in Depth-chapter II, "The Semantics of constructors"

Source: Internet
Author: User
The contents of the second chapter, after a total of reference review. Note: The following part of the picture from the original book

1. The default constructor is generated by the compiler when needed , paying attention to the word " when needed ", such as the following code:

Class bat{public
:
	int val;
	bat* Pnext;
	void Bat_yell ();

void Bat::bat_yell ()
{
	Bat batMan;
	if (Batman.val | | batman.pnext)
		//... do something
	//...		
}

According to the programmer's intention, in the Bat_yell () method, Batman invokes the default constructor to initialize Val and pnext to 0, but the actual result is not that the programmer's needs do not represent the needs of the compiler, and of course, the responsibility of the programmer should not be borne by it, in the final analysis , there is no default constructor to be synthesized in the preceding code fragment. To step back, even if a default constructor is synthesized, the above data member is not initialized to 0. That is, the default constructor at this time is trivial (useless, unimportant), and in four cases the compiler produces a nontrivial (important, useful) default constructor:

A. Class member objects with default constructors:


Class member object Foo of bar contains a default constructor, so the compiler will synthesize the default constructor for class bar, which might look like this:

It is noteworthy that even the compiler has synthesized the default constructor for us, but it does not produce any code to initialize the bar's STR member variable, which is the responsibility of the programmer. So what does the compiler do if the programmer provides a constructor (default or with a parameter)? The answer is that the compiler expands later constructors, placing code in each constructor, and the code is placed in front of the programmer's code, making each constructor call the default constructor for each member variable with a default constructor, a little bit around, and see a concrete example:
B. The second scenario is the default constructor in the base class, similar to the first case, if a class that does not have a default constructor is inherited (derived) from a base class with a default constructor, the default constructor for that subclass is synthesized, in which the constructors of the base class are called separately in the order of Declaration. If a class designer provides more than one constructor, but does not have a default constructor, the compiler expands the existing constructor without synthesizing a new default constructor. If this class also contains the first case: a member class object that has a default constructor, their default constructor is called after the base class default constructor is invoked. C. Classes with virtual functions, because of the existence of virtual functions, virtual function tables and virtual table pointers are generated, which must be arranged during object construction, so for each constructor defined by the class, the compiler will place code to do such things, and for any constructor class that is not declared, The compiler generates a default constructor for them to properly initialize the virtual table pointers for each class object. D. Classes that inherit from the virtual base class, because a pointer to a virtual base class exists in the virtual inheritance subclass, similar to the third case.
In summary, the above four situations compilers synthesize an implicit, important default constructor (implicit nontrivial default constructor) to meet the needs of the compiler (not the program): Invoke the Member class or the default constructor of the base class. or complete each object to initialize its virtual function mechanism or virtual base class mechanism. For other cases and classes that do not declare any constructors, they have implicit, unimportant default constructors (implicit trivial default constructor) that are not actually synthesized. It is important to note that the resultant default constructor initializes only the necessary portions of the compiler's view (base class object, Class object), and other data members are not initialized.
Finally, remember two myths (allNo): 1. Any class that is not defined as a default constructor is synthesized. 2. The default constructor synthesized by the compiler sets the default value for each member within the class. In the next article, collate the contents of the copy constructor (copy constructor).

Related Article

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.