Differences between C # And C ++ in static Constructor

Source: Internet
Author: User

In C #, the static constructor of a class is used for initialization before the class is used. For example, it initializes static members or performs specific operations. The CLR automatically calls the static constructor when it first creates the class object or calls the class static method. At the same time, CLR ensures the thread security of the static Constructor (specifically, it is called only once, and there is no multithreading problem ).

The following describes the characteristics of static constructor in MSDN:

1. The static constructor neither has an access modifier nor a parameter

2. Before creating the first instance or referencing any static member, the static constructor is automatically called to initialize the class.

3. The static constructor cannot be called directly.

4. In the program, users cannot control when to execute static constructor.

The C ++ language specification does not include anything similar to a static constructor, but the requirement for initialization before using classes exists objectively. To meet the requirements, C ++ can be implemented manually, but initialization time and thread security should be well handled. This article attempts to simulate static constructor through the C ++ template mechanism to avoid the tedious Implementation of manual initialization. For Class A that requires A static constructor, you only need to inherit the static_constructable template class and provide the static void statici_constructor () static method:

Output:

The following is the implementation of the static_constructable class template:

In the above implementation, the callback for A: static_constructor () is put into the constructor of the internal class helper, and A helper local static variable is defined in static_constructable; C ++ ensures that the base class static_constructable is called before constructing the object of the derived class A, and the static local variable is constructed only once, in this way, A: static_constructor () is called once and only once.

The static_constructor class template simulates the static constructor mechanism of c #, which has the following features:

1. automatically call the static constructor provided by the class before constructing the class object for the first time.

2. The time when the static constructor is called is determined.

3. the local static variable initialization mechanism of c ++ ensures thread security (correct: it is not thread security, and the C ++ standard does not involve multithreading, generally, the implementation of the compiler is NOT thread-safe. For more information, see the comments section)

4. The Inheritance-based implementation mechanism does not change the object memory layout of the derived class.

However, compared with the features of the C # static constructor listed in this article, this implementation has obvious shortcomings: the static constructor cannot be triggered by calling the static method of Class; the static constructor of Class A must be public.

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.