On the difference between C # and C + + on static constructors

Source: Internet
Author: User
Tags constructor thread

In C #, the static constructors of a class are used to perform related initialization work before the class is used, such as initializing static members or performing specific actions. The CLR automatically calls the static constructor the first time the class object is created or when the class static method is invoked. At the same time, the CLR guarantees the thread security of the static constructor (precisely, only once, without multithreading problems).

The following is an MSDN description of the static constructor features:

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

2. A static constructor is automatically invoked to initialize a class before the first instance is created or any static members are referenced.

3. Cannot call a static constructor directly

4. In a program, users cannot control when a static constructor is executed

The C + + language specification does not contain anything similar to a static constructor, but the need to do initialization before using a class is objective. In terms of satisfying the demand itself, C + + can be implemented manually, but to deal with the initialization time, thread security and other issues. This paper attempts to simulate the static constructor through the template mechanism of C + +, avoiding the tedious implementation of manual initialization. For Class A, which requires a static constructor, simply inherit the Static_constructable template class and provide a static void Statici_constructor () Statics method:

Output:

The following is the implementation of the Static_constructable class template:

The above implementation places a callback for A::static_constructor () into the constructor of the internal class helper, and defines a helper local static variable in static_constructable (); C + + guarantees that when the object of a derived class A is constructed, the constructor of the base class static_constructable is invoked, and the static local variable is only constructed once, so that the purpose of the call once and only once A::static_constructor () is reached.

The Static_constructor class template simply simulates the static constructor mechanism of C #, which has the following characteristics:

1. Automatically invoke the static constructor provided by the class before the class object is first constructed

2. The timing of the static constructor invocation is OK

3. The use of C + + local static variable initialization mechanism to ensure thread security (correction: not actually thread-safe, C + + standard does not involve multithreading issues, and the general compiler implementation is not thread-safe, see the comments section)

4. The implementation mechanism based on inheritance does not change the object memory layout of derived classes

However, compared with several features of the C # static constructors listed in this article, this implementation has a significant disadvantage: the static constructor cannot be triggered by invoking the static method of Class A; 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.