Under what circumstances will the C + + compiler generate the default constructor __jquery

Source: Internet
Author: User

Question: for C + + beginners There is an error, if the class does not define any constructors, the compiler will automatically generate the default constructor.

NOTE: This statement is wrong.

The correct argument is that the compiler will not synthesize the default constructor until the default constructor is "required" .

When is "needed" in that case.

There are four scenarios in which the compiler will automatically synthesize the default constructor

Situation 1 contains a class object data member with a default constructor for the class object type

That is: A class contains the object data member of the object, which has a default constructor SA contains an object member OBJECTB, and this object member defines a (CLASSB) default constructor, then the compiler synthesizes a default constructor for ClassA.

Class A
{public
:
	A ()
	{
		cout << "A ()" << Endl;
	}
};
Class B
{public
:
	a A;
	int num;
};
void Test ()
{
	B;
	cout << b.num << endl;
}

int main ()
{
	Test ();
	return 0;
}

Reason: Because the class member object has a default constructor, the compiler needs to explicitly invoke the constructor of the class member object. And the compiler wants to explicitly invoke the default constructor of the class member object, and you need to synthesize some code to invoke it. But remember, the compiler-synthesized default constructor simply invoke the default constructor of the class member object and do not initialize any other variables in our class .

Case 2 derived class with default constructor for base class

When a class derives from a base class that contains a default constructor, the class also conforms to the criteria that the compiler needs to synthesize the default constructor. The compiler-synthesized default constructor calls the top-level base class default constructor based on the order in which the base class is declared.

Reason: Because a derived class is synthesized, it needs to explicitly call the default constructor of the base class.

Class Base
{public
:
	base ()
	{
		cout << "base ()" << Endl;
	}
};

Class Derived:p ublic Base
{public
:
	int D;
};

void Test ()
{
	Derived D;
}

int main ()
{
	Test ();
	return 0;
}

Scenario 3 classes with virtual functions

classes can be divided into two cases with virtual functions:

The

(1) class itself defines its own virtual functions             (2) classes inherit virtual functions from the inheritance system (once a member function is declared as a virtual function, Inheritance does not change the "virtual property" of a virtual function.
        Both of these situations make a class with a virtual function. Such a class also satisfies a class that the compiler needs to synthesize a default constructor because class objects with virtual functions contain a virtual table pointer vptr , and the compiler needs to set an initial value on vptr to satisfy the proper operation of the virtual function mechanism, The compiler places this initial operation in the default constructor . If the designer does not define any default constructors, the compiler will synthesize a default constructor to do so, otherwise the compiler will insert code into each constructor to do the same thing. The
case 4  class       Virtual inheritance with a virtual base class is also synthesized in a subclass object A pointer to the virtual base class and therefore initialized, so you must construct the function. Virtual base class or virtual inheritance guarantees that only one object of a virtual base class is in a subclass object.      To summarize that:  does not conform to the above 4 scenarios and does not declare any constructors, the compiler does not synthesize the default constructor;                     and the synthesized default constructor does not initialize the data members of the class's built-in type composite type.


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.