"Deep Exploration C + + object Model" Reading notes (2)

Source: Internet
Author: User
Tags constructor object model

The default constructor is synthesized only when the compiler needs it.

Generally, the default constructor that is synthesized by the compiler is useless (trivial), with the following exceptions:

(1) member Class Object with "Default constructor"

If a class does not have any constructor, but it contains a member object, and the latter has a default constructor, then the compiler does not synthesize a "class" when constructor really needs to be called. Nontrivial "the default constructor. To avoid the synthesis of multiple default constructor, the solution is to synthesize the default constructor, copy constructor, destructor, assignment copy Operator are done in a inline way. A inline function has a static linkage that is not visible to people outside the file. If the function is too complex to be made into a inline, a explicit non-inline static entity is synthesized.

According to the guidelines "If Class A contains one or more member class objects, then each constructor of class A must invoke the default constructor of each member classes", Even for a user-defined default constructor, the compiler expands it, placing the default associated with each member by the "explicit" in the order in which they are declared before the username code ("Objects in Class") constructor.

class Dcpey   { public:Dopey(); ... };
class Sneezy  { public:Sneezy(int); Sneezy(); ... };
class Bashful { public:Bashful(); ... };

class Snow_White {
public:
Dopey dopey;
Sneezy sneezy;
Bashful bashful;
// ...
private:
int mumble;
};

Snow_White::Snow_White() : sneezy(1024)
{
mumble = 2048;
}
// 编译器 扩张后的default constructor
Snow_White::Snow_White() : sneezy(1024)
{
// 插 入member class object
// 调用其constructor
dopey.Dopey::Dopey();
sneezy.Sneezy::Sneezy();
bashful.Bashful::Bashful();

// explicit user code
mumble = 2048;
}

(2) "with default constructor" Base Class

If a class that does not have any constructors derives from a base class "with default constructor", then the default constructor of this derived class is considered Nontrivial, and therefore need to be synthesized out.

It is to be noted that the compiler will place these base class constructor before the member object.

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.