C + + constructor semantics--where does the compiler synthesize default Constructot

Source: Internet
Author: User

C + + beginners often misunderstand:

C + + Newbies generally have two common misconceptions:

1. If the default constructor is not defined for any class, it will be synthesized from one.

2. The default constructor compiled by the compiler will display the setting "defaults for each data member in class"

There are four situations that cause the compiler to synthesize a defaultconstructor for undeclared constructor classes, and C + + Standan to make those compositions implicit nontrivial default constructors, the synthesized constructor intelligently satisfies the needs of the compiler (not the program) and is able to accomplish its task by "invoking the default constructor of member object or base class" or " Initializes the virtual function mechanism or the virtual base class mechanism for each object to be completed. As for the absence of those four cases without declaring any constructor of the classes. We say they have implict trivial default constructor (implicitly useless default constructors) that are not actually synthesized.

In the synthesized defaultconstructor, only the base class Subobjects and Member class object are initialized. All other nonstatic data member, such as integers, integer pointers, integer arrays, and so on, are not initialized, which may be required for the initialization of the operations team, but not necessary for the compiler, if the program requires a default of "set a pointer to 0" constructor, then the person who provided him should be a programmer.


For a class, if there is no declaration of any constructors, then a default constructor is implicitly declared. An implicitly-declared default constructor is the trivial constructor (useless constructor). But when the compiler needs it, it synthesizes a Nontrivialdefault constructor. There are four situations in which the nontrivial default constructor is synthesized. (useful default construction of the HA number)

1. Member class object with Defaultconstructor

If a class does not have any constructor, but it contains a member object, and the latter has defaultconstructor, then this class's implicit default constructor is nontrivial, the compiler needs to synthesize a default constructor for the class. However, synthetic operations occur only when the constructor really needs to be called.

How to avoid synthesizing multiple default constructor in each C + + module: The solution is to synthesize the Defaultconstructor, copy constructor, assignment copy The operator are all done in inline mode. An inline function has a static link that is not visible to the outside of the file. If the function is too complex to fit inline, a explicitnon-inline static instance is synthesized. For example:

class Foo {public:foo (), foo (int) ...};

Class Bar {Public:foo Foo; char *str;};

Bar Bar; Bar::foo must be initialized here, Bar::foo is a memberobject, and has default//constructor. So the compiler synthesizes defaultconstructor.

Here, it is the responsibility of the compiler to initialize Bar::foo, and it is the responsibility of the programmer to initialize BAR::STR. So the synthesis

The default constructor looks like this:

Inline Bar::bar () {

Foo. Foo::foo ();

}

But if the programmer provides a default constructor, the following:

Bar::bar () {str= 0;}

Because a default constructor already exists, the compiler is not able to synthesize a second. The compiler's action is: If the class contains one or more member class objects, then each constructor of the class must call classes of each member Defaultconstructor The compiler will extend the existing constructors, where some code is placed, and the necessary defaultconstructor is called before user code is executed.

Then the above expansion may look like this:

Bar::bar () {

Foo. Foo::foo ();

str = 0;

}

If more than one class member objects requires constructor initialization, the C + + language requires member objects to invoke each constructors in the order declared in class. This is done by the compiler, which constructor a code program for each of the member, invoking the default constructors associated with each member in the order in which they are declared. The code will be placed before the explicit user code.

2. Base class with Defaultconstructor

If a class without any constructor derives from a base class with default constructor, then the default constructor is considered nontrivial and therefore needs to be synthesized. It will call the defaultconstructor of the previous layer base class (according to their order of declaration).

If there are multiple constructors, but none of them are defaultconstructor, the compiler expands the first constructors, adding the program code to invoke all the necessary default constructors. If there is also member class objects with default constructor, those default constructors will be called after all base class constructors are called.

3. Class with a virtualfunction (declaration or inheritance)

Class widget{

Public

Virtualvoid Flip () = 0;

};

void Flip (const widget & widget) {widget.flip ();}

Suppose Bell and whistle are derived classes of widgets

void Foo () {

BELLB;

Whistle W;

Flip (b);

Flip (w);

};

The following two expansion actions occur during compilation:

1) A virtual function table (VTBL) will be generated in the compiler to put the virtual function address of class.

2) In each class object, an additional pointermember is synthesized by the compiler and contains the associated CLASSVTBL address.

In addition, the virtual call operation of Widget.flip () is rewritten to use the flip () entry in the widget's vptr and VTBL.

(*widget.vptr[1]) (&widget);

In order for this mechanism (virtual mechanism) to function, the compiler must set the initial value for the vptr of each Widget object and place the appropriate virtual table address. For each constructor defined by class, the compiler will have some code to do such things (see Section 5.2). For those classes that do not declare any constructors, the compiler will synthesize a defaultconstructor for them to properly initialize the vptr of each class object.

4. Class with a virtual BaseClass

The implementation of the Virtual base class differs greatly from one compiler to another. However, the common feature of each implementation is that the location of the virtual base class in each of its DerivedClass object can be prepared at the execution time. For example:

Class X {public:int i;};

Class A:public virtual X {public:int J;};

Class B:public virtual X {public:doubled;};

Class C:public A, public B {PUBLIC:INTK;};

void foo (const A * pa) {pa->i = 1024;} No longer compile time determines the location of the PA->X::I

Main () {

Foo (new A);

Foo (new C);

}

The compiler cannot fix the actual offset position of "x::i accessed via Pa" in Foo () because the true type of PA can be changed. The compiler must change the code that performs the access operation, and the x::i can be deferred until the execution period is determined. The original Cfront was done by placing a pointer in each virtual baseclasses of the derived class object. All manipulations that "access a virtualbase class via reference or pointer" can be done with relevant pointers. In my case, foo () can be rewritten as follows to conform to this strategy:

Possible compiler transition actions

void foo (CONST * PA) {pa->_vbcx->i= 1024;}

Where _VBCX represents a pointer produced by the compiler, pointing to Virtualbase class X.

_VBCX (or something that the compiler makes) is done during class object construction. For each constructor defined by class, the compiler will insert code that "allows execution-time access to every virtual base class." If class does not declare any constructors, the compiler must synthesize a default constructor for it.


C + + constructor semantics--where does the compiler synthesize default Constructot

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.