C + + Constructor FAQ continue

Source: Internet
Author: User

This chapter to understand what a compiler declaration is only a default construct. This also explains why the same seemingly meaningless definition, if not also declares the meaning of the default constructor.


Q: When the compiler implicitly defines a default constructor.


A: An implicitly-declared default constructor only implicitly defines a default constructor when the compiler needs it. It is only necessary for such things as the following.

1. A class with virtual member functions. A class with a virtual member function must have a non-trivial constructor. A non-trivial constructor may be either user-defined or implicitly-defined by the compiler. It is also responsible for providing correct initialization for addresses such as vptr.

2. Member Class Object with default Constructor

3. Base Class with default constructor

4. Class with a virtual Bass class

See above for details (explore the C + + object model in depth)


Q: Why does the compiler implicitly declare trivial constructors even if they are never defined?

Answer: Under certain conditions. The compiler implicitly declares a default constructor but does not define it. This constructor is called the trivial. Very many C + + program apes are very confused about the concept of trivial member functions.

When the compiler knows that a function is not defined by default, why is the compiler implicitly declaring a default constructor? What is the purpose of doing this?

The first thing to keep in mind is that the implicit declaration is conceptually, and the compiler does not actually insert the so-called declaration code inside your code. More precisely, the compiler, the connector, and the program behave as if the constructor had been declared. In fact, the compiler simply sets up a declaration in the class's type information record that BITS is used to indicate that their so-called type has a default constructor.

Now let's go back to the most critical question, why bother with an implicit statement? After all, C does not need to provide this mechanism for its struct or union. The fact is that implicit declarations have a contractual rule. Each implicit declaration is like a clause in a contract that declares how a certain class is used.

When the compiler implicitly declares this special member function, it grants some authorization to the user, in contrast, assuming that the compiler does not implicitly declare member functions, which restricts the user's use of the class. Consider the following code:

struct blocked{public:blocked (const blocked&);};
Now you must know. Because of the presence of Copy-constructor, the compiler is not implicitly declared.

Since this class does not implicitly or explicitly declare a default constructor, you cannot instantiate a class member as follows.

Blokced b; Error, no default constructor availableblocked *p = new Blocked; Error
There is no implicit declaration mechanism. Program apes have to manually add constructors, copy constructors, assignment functions, and destructors for each class that needs to be instantiated. The following pod types attest to this view:

struct Date{int day, month, year;};
The compiler diet declares the following member functions, for example:

    • A trivial default constructor
    • A Trivial copy constructor
    • A Trivial assignment operator
    • A Trivial destructor
These statements allow you to use date as follows:

Date D; Implicit declaration of default ctor and Dtor allow Thisdate * pdate= new Date; Same heredate D2 (d); Implicit copy ctor declaration allows this*pdate=d2; Implicit assignment operator declaration allows Thisdelete pdate; Implicit Dtor declaration allows this

The trivial member function is important in C + + programming. To make it clear how much trouble it can save you, try to show them the declaration:

struct Date{int day, month, Year;private: ~date ()//declared but not defined Date (const date&);//ditto};

Date D; Error, no default ctordate * pdate= new Date; Same error heredate D2 (d); Error, no accessible copy ctor

The code above cannot be compiled now.



C + + Constructor FAQ continue

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.