C + + simulation of C # static functions

Source: Internet
Author: User
Tags assert

Http://developer.51cto.com/art/201002/181969.htm

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 features of the C + + static constructor:

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< A> template class and provide a static void Statici_constructor () Statics method:

class a : static_constructable< a>  {  Public:   STATIC&NBSP;VOID&NBSP;STATIC_ Constructor ()  {  std::cout < <   "Static constructor a"  < < &NBSP;STD :: Endl;   s_string =  "ABC";  //initialization static data  }   static std::string s_string;   Public:   A () {  std::cout < <   "Constructor a"  < <  std::endl; & nbsp }   Private:   int m_i;  };   std::string a::s_string;   Int _tmain (int argc, _tchar* argv[]) {  std::cout < <   "Beginning  of main " < <  std::endl;   Assert (sizeof (A)  == sizeof (int));//inheritance does not change the memory layout   assert (a::s_string ==  "") of A;   a a1;   ASSERT (a::s_string ==  "abc");   a a2;   std::cout < <   "End of main"  < <  std::Endl   return 0;  } 

Output:

Beginning of main static constructor A//The static construction method is automatically invoked before a object is created, once and only once constructor a constructor a end of main

The following is the implementation of the Static_constructable class template:

template< TypeName T> class Static_constructable {private:struct helper{helper () {T::static_constructo   R ();   }   };    Protected:static_constructable () {static helper placeholder; }   };

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< a> (); C + + guarantees that when constructing an object of derived class A, the base class static_constructable< A> constructor is invoked, and the static local variable is only constructed once, so that it can be invoked once and only once A::static_constructor (). </span>

The Static_constructor class template simply simulates the static constructor mechanism of C #, and C + + static constructors have 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.

The above is the introduction of the C + + static constructor.

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.