[Windows編程] #pragma once 和#ifndef … #define … #endif 比較

來源:互聯網
上載者:User

C++中防止標頭檔被多次include 的常見方式有:

1) 用#ifndef ...  #define ... #endif  宏

 

#ifndef __MYHEADER_H__
#define __MYHEADER_H__

 

// Here is my class ...

#endif 

 

2) 用 #pragma once

#pragma once

// Here is my class ...

 

建議使用 #pragma once ,原因:

1)  代碼簡潔,維護性比較好。 #pragma once 顯然比 #ifndef ...  要簡短許多,而且避免了__MYHEADER_H__ 重定義 或者 #endif 包含範圍錯誤的情況

 

2) 編譯速度快。  使用 #ifndef 的話,編譯器每次看到#include這個檔案都需要讀入檔案,解析代碼。  而使用#pragma once 編譯器根本不會重複開啟檔案, 大大提高了效率。

以上兩點在大型C++工程中表現得尤為突出。

 

註: #pragma once 不是國際標準, 只是微軟C++ 的一個功能, 但現在多數主流C++編譯器也都實現了這個功能。

 

 

 >> 原創文章的著作權屬於作者,轉載請註明出處和作者資訊(http://blog.csdn.net/WinGeek/), 謝謝。 <<

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.