In-depth exploration of C ++ static Constructor

Source: Internet
Author: User

The primary table is the address table of a class's virtual function. This table solves the inheritance and overload problems and ensures that it can reflect the actual function in a real way, this C ++ static constructor belongs to a class, rather than an instance. That is to say, this constructor will only be executed once. That is, it is automatically called by. NET before creating the first instance or referencing any static members.

Pay attention to the following points when using static constructors:

1. The static constructor neither has an access modifier nor a parameter. Because it is called by. NET, modifiers such as public and private are meaningless.

2. When the first class instance or any static member is referenced ,. NET will automatically call the static constructor to initialize the class. That is to say, we cannot directly call the static constructor, so we cannot control when to execute the static constructor.

3. A class can have only one C ++ static constructor.

4. A constructor without parameters can coexist with a C ++ static constructor. Although the parameter list is the same, one belongs to the class and the other belongs to the instance, so there is no conflict.

5. Run only once at most.

6. Static constructors cannot be inherited.

7. If the C ++ static constructor is not written and the class contains a static member with an initial value, the compiler automatically generates the default static constructor.

  1. Differences between standard input implementation methods in C and C ++
  2. How does the C ++ compiler allocate storage space for Const constants?
  3. Basic Conception and method of C ++ Class Library Design
  4. Several Methods for converting C ++ Language
  5. How to better compile C ++ code

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.