C + + Constructor FAQ cont.

Source: Internet
Author: User

In this section, let's see when the compiler declares and defines a default constructor. It also explains why it does not seem to make sense to declare the meaning of a default constructor even if you do not give the definition.


Q: When does the compiler implicitly define a default constructor.


A: An implicitly declared default constructor implicitly defines a default constructor only when required by the compiler. It is necessary only if the following conditions are true.

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. And it has the responsibility to provide correct initialization for for example the address of the 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?

A: Under certain conditions, the compiler implicitly declares a default constructor but does not define it. Such a constructor is called a trivial. Many C + + programmers are very confused about the concept of trivial member functions.

Why does the compiler implicitly declare a default constructor when the compiler knows that a function is not defined by default? 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 some bits in the type information record for the class to indicate that they have a default constructor for their so-called type.

Now let's go back to the most critical question, why bother with an implicit statement? After all, C does not need to provide such a 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, if the compiler does not implicitly declare member functions, it restricts the user's use of the class. Consider the following code:

struct blocked{public:blocked (const blocked&);};
Now you must know that because the existence of copy-constructor makes the compiler not implicitly declared. Because 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
Without an implicit declaration mechanism, programmers 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:

    • 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 very important in C + + programming, in order to show you how much trouble it can save you, try to display the declaration of them:

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

Now the code above cannot be compiled.



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.