自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

談C#與C++在靜態(tài)構(gòu)造函數(shù)上的區(qū)別

開發(fā) 后端
靜態(tài)構(gòu)造函數(shù)用于初始化任何靜態(tài)數(shù)據(jù),或用于執(zhí)行僅需執(zhí)行一次的特定操作。在創(chuàng)建第一個(gè)實(shí)例或引用任何靜態(tài)成員之前,將自動(dòng)調(diào)用靜態(tài)構(gòu)造函數(shù)。希望對你有幫助,一起來看。

C#中,類的靜態(tài)構(gòu)造函數(shù)用于在使用類之前進(jìn)行相關(guān)的初始化工作;比如,初始化靜態(tài)成員或執(zhí)行特定操作。CLR 在***次創(chuàng)建該類對象或調(diào)用該類靜態(tài)方法時(shí)自動(dòng)調(diào)用靜態(tài)構(gòu)造函數(shù)。同時(shí),CLR保證靜態(tài)構(gòu)造函數(shù)的線程安全性(準(zhǔn)確地說是,只會(huì)調(diào)用一次,不存在多線程問題)。

下面是MSDN對靜態(tài)構(gòu)造函數(shù)特點(diǎn)的描述:

1.靜態(tài)構(gòu)造函數(shù)既沒有訪問修飾符,也沒有參數(shù)

2.在創(chuàng)建***個(gè)實(shí)例或引用任何靜態(tài)成員之前,將自動(dòng)調(diào)用靜態(tài)構(gòu)造函數(shù)來初始化類

3.無法直接調(diào)用靜態(tài)構(gòu)造函數(shù)

4.在程序中,用戶無法控制何時(shí)執(zhí)行靜態(tài)構(gòu)造函數(shù)

C++語言規(guī)范并未包含類似靜態(tài)構(gòu)造函數(shù)的東西,但在使用類之前做初始化工作的需求卻是客觀存在的。就滿足需求本身來講,C++完全可以通過手動(dòng)方式實(shí)現(xiàn),但要處理好初始化時(shí)機(jī),線程安全性等問題。本文則嘗試通過C++的模版機(jī)制模擬實(shí)現(xiàn)靜態(tài)構(gòu)造函數(shù),避免手動(dòng)初始化的繁瑣實(shí)現(xiàn)。對于需要靜態(tài)構(gòu)造函數(shù)的類A,只需用繼承static_constructable<A>模版類,并提供 static void statici_constructor()靜態(tài)方法即可:

  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”; //初始化靜態(tài)數(shù)據(jù)  
  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));//繼承不改變A的內(nèi)存布局  
  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;  

輸出:

  1. beginning of main  
  2. static constructor a //創(chuàng)建A對象前自動(dòng)調(diào)用靜態(tài)構(gòu)造方法,一次且僅一次  
  3. constructor a  
  4. constructor a  
  5. end of main 

下面是static_constructable類模板的實(shí)現(xiàn):

  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. }; 

上面的實(shí)現(xiàn)把對A::static_constructor()的回調(diào)放到內(nèi)部類helper的構(gòu)造函數(shù)中;并在static_constructable<A>()中定義一個(gè)helper局部靜態(tài)變量;C++保證在構(gòu)造派生類 A的對象時(shí),會(huì)先調(diào)用基類static_constructable<A>的構(gòu)造函數(shù),且靜態(tài)局部變量只會(huì)構(gòu)造一次,這樣就達(dá)到調(diào)用一次且僅一次A::static_constructor()的目的。

static_constructor類模板簡單地模擬了C#的靜態(tài)構(gòu)造函數(shù)機(jī)制,它具有以下特點(diǎn):

1. 在***次構(gòu)造類對象之前自動(dòng)調(diào)用類提供的靜態(tài)構(gòu)造函數(shù)

2. 靜態(tài)構(gòu)造函數(shù)被調(diào)用的時(shí)機(jī)是確定的

3. 利用了C++的局部靜態(tài)變量初始化機(jī)制保證了線程安全性(更正:實(shí)際并非線程安全,C++標(biāo)準(zhǔn)不涉及多線程問題,而一般編譯器實(shí)現(xiàn)也非線程安全,更多參見評論部分)

4. 基于繼承的實(shí)現(xiàn)機(jī)制并未改變派生類的對象內(nèi)存布局

不過,和本文開始列出的C#靜態(tài)構(gòu)造函數(shù)的幾個(gè)特點(diǎn)相比,本實(shí)現(xiàn)還有明顯的不足:無法通過調(diào)用類A的靜態(tài)方法觸發(fā)靜態(tài)構(gòu)造函數(shù);類A的靜態(tài)構(gòu)造函數(shù)必須是public的。

【編輯推薦】

  1. C#初學(xué)者對Equals方法的幾個(gè)常見誤解
  2. c#.net實(shí)體類序列化方法
  3. 深入C# 序列化(Serialize)、反序列化(Deserialize)
  4. C#中自增、自減操作符重載是個(gè)怎么回事兒
  5. C#中的閉包是怎么捕獲變量的
責(zé)任編輯:于鐵 來源: IT168網(wǎng)站
相關(guān)推薦

2011-06-11 21:36:44

C#C++

2009-11-17 09:07:55

靜態(tài)構(gòu)造函數(shù)

2009-08-13 17:30:30

C#構(gòu)造函數(shù)

2009-07-31 15:37:45

C#靜態(tài)構(gòu)造函數(shù)

2009-08-20 14:28:00

C#靜態(tài)構(gòu)造函數(shù)

2009-07-31 15:44:02

C#靜態(tài)構(gòu)造函數(shù)

2010-01-27 16:10:32

C++靜態(tài)構(gòu)造函數(shù)

2009-08-13 18:02:11

C#靜態(tài)構(gòu)造函數(shù)

2010-01-22 11:13:16

C++靜態(tài)

2024-12-31 00:07:12

2009-09-17 18:56:22

CLR Via C#

2025-02-06 13:23:09

C++函數(shù)參數(shù)

2010-02-01 11:01:30

C++靜態(tài)構(gòu)造函數(shù)

2010-01-27 17:16:52

C++構(gòu)造函數(shù)

2009-07-30 15:24:13

C#析構(gòu)函數(shù)C#構(gòu)造函數(shù)

2009-07-31 14:15:38

C# 構(gòu)造函數(shù)

2009-08-24 18:09:13

C#構(gòu)造函數(shù)

2009-10-23 11:31:05

CLR Via C#調(diào)

2009-08-13 18:10:31

C#靜態(tài)構(gòu)造函數(shù)

2009-08-13 17:38:42

C#構(gòu)造函數(shù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號