Analysis of concepts related to C ++ static Constructor

Source: Internet
Author: User

How can we correctly understand C ++ static constructor? What role does this static function play in practical applications? You will find these answers in this article. I hope this content will help you better understand C ++.

  • Summary of C ++ TinyXML usage
  • Two types of C ++ Data Pointer
  • Introduction to the specific application functions of C ++ File Operations
  • C ++ cross-platform application compilation methods
  • How to correctly understand the C ++ framework

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. It is called only once, and there is no multithreading problem ).

The following describes the characteristics of C ++ 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 <A> template class and provide the static void statici_constructor () method:

 
 
  1. Class A: static_constructable <A>
  2. {
  3. Public:
  4. Static void static_constructor (){
  5. Std: cout <"static constructor a" <std: endl;
  6. S_string = "abc"; // initialize static data
  7. }
  8. Static std: string s_string;
  9. Public:
  10. A (){
  11. Std: cout <"constructor a" <std: endl;
  12. }
  13. Private:
  14. Int m_ I;
  15. };
  16. Std: string A: s_string;
  17. Int _ tmain (int argc, _ TCHAR * argv []) {
  18. Std: cout <"beginning of main" <std: endl;
  19. Assert (sizeof (A) = sizeof (int); // inheritance does not change A's memory Layout
  20. Assert (A: s_string = "");
  21. A a1;
  22. Assert (A: s_string = "abc ");
  23. A a2;
  24. Std: cout <"end of main" <std: endl;
  25. Return 0;
  26. }

Output:

 
 
  1. Beginning of main
  2. Static constructor a // The static constructor is automatically called once and only once before object A is created.
  3. Constructor
  4. Constructor
  5. End of main

The following is the implementation of the static_constructable class template:

 
 
  1. template< typename T> 
  2. class static_constructable  
  3. {  
  4. private:  
  5. struct helper{  
  6. helper(){  
  7. T::static_constructor();  
  8. }  
  9. };  
  10. protected:  
  11. static_constructable(){  
  12. static helper placeholder;   
  13. }  
  14. }; 

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 <A>; C ++ makes sure that when constructing the object of the derived class A, the base class static_constructable <A> constructor is called first, and the static local variable is constructed only once, in this way, A: static_constructor () is called once and only once. </Span>

The static_constructor class template simulates the static constructor mechanism of c #. C ++ static constructor 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.

The above describes the C ++ static constructor.

Related Article

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.