`
阿尔萨斯
  • 浏览: 4172799 次
社区版块
存档分类
最新评论

#pragma once 和#ifndef ... #define ... #endif 比较

 
阅读更多

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++编译器也都实现了这个功能。



分享到:
评论

相关推荐

    全面了解#pragma once与 #ifndef的区别

    2 #pragma once方式 在能够支持这两种方式的编译器上,二者并没有太大的区别,但是两者仍然还是有一些细微的区别。 方式一: #ifndef __SOMEFILE_H__ #define __SOMEFILE_H__ … … // 一些声明语句 #endif 方式二...

    C++ 中pragma once 与 #ifndef _XXX_H_ #define _XXX_H_的区别

    C++ 中pragma once 与 #ifndef _XXX_H_ #define _XXX_H_的区别 pragma once 之前一直用的很好,今天和同事的配合中发现自己没有真正理解pragma once。 原因:同事喜欢把公共的头文件通过生成后事件复制到一个公共的...

    mtk 下载工具源码,研究代码挺好的东西

    #ifndef SPMETA_DLL_H #include "spmeta.h" #endif #ifndef _SLA_CHALLENGE_H_ #include "sla_challenge.h" #endif #ifndef _MULTIPORT_COMMON_H_ #include "Multiport_Common.h" #endif #ifndef _FORM_MULTILOAD...

    C++EGE: Ballon Ball 动画

    #pragma once #endif #ifndef __cplusplus #error You must use C++ compiler, or you need filename with '.cpp' suffix #endif #if defined(_INC_CONIO) || defined(_CONIO_H_) #error can not include "conio.h...

    运动会分数统计 C++

    #pragma once #endif #ifndef _INC_EH #define _INC_EH #if !defined(_WIN32) && !defined(_MAC) #error ERROR: Only Mac or Win32 targets supported! #endif #ifdef _MSC_VER // Currently, all MS C ...

    externC_ifndef_define_endif_pragma onceDemo.zip

    c++/c中 externC. ifndef define endif 的用法

    PIC 芯片驱动 8563时钟的程序

    //#pragma once #ifndef _clock_8563_pic24FJ #define _clock_8563_pic24FJ #define SDA_OUT LATDbits.LATD9 #define SDA_IN PORTDbits.RD9 #define SDA_DDR TRISDbits.TRISD9 #define SCK ...

    UseLib.zip

    使用这个小工具,可将指定目录及其子目录下的全部LIB文件自动生成为含完整路径的“#pragma comment(lib,”语句,不需要在VS环境设置LIB目录。 示例: #ifndef D__VTK820_Debug_lib #define D__VTK820_Debug_lib ...

    listview_5.zip

    #pragma once #endif // _MSC_VER >= 1000 #define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers #include <afxwin.h> // MFC core and standard components #include <afxext.h> // MFC ...

    C语言 时钟

    #pragma once #endif // _MSC_VER > 1000 #define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers #include <afxwin.h> // MFC core and standard components #include <afxext.h> // MFC ...

    vc StdAfx头文件

    #pragma once #endif // _MSC_VER > 1000 #define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers #include <afxwin.h> // MFC core and standard components #include <afxext.h> // MFC ...

    XPButton控件

    #pragma once #endif // _MSC_VER >= 1000 // Return values #ifndef BTNST_OK #define BTNST_OK 0 #endif #ifndef BTNST_INVALIDRESOURCE #define BTNST_INVALIDRESOURCE 1 #endif #ifndef BTNST_FAILEDMASK #...

    HK StreamMediaPlayer DEMO

    浙江海康威视硬盘录像机播放器DEMO / stdafx.h : 标准系统包含文件的包含文件, // 或是经常使用但不常...#pragma once #ifndef VC_EXTRALEAN #define VC_EXTRALEAN // 从 Windows 标头中排除不常使用的资料 #endif

    tree代码的实现与简单应用

    #pragma once #endif // _MSC_VER > 1000 #ifndef __AFXWIN_H__ #error include 'stdafx.h' before including this file for PCH #endif #include "resource.h" // main symbols //////////////////////////////...

    glsl version 300 es 预处理

    #if #define #undef #if #ifdef #ifndef #else #elif #endif #define USE_LIGHT #define LIGHT_NUMBER 10 ... #ifdef USE_LIGHT .... #else ... #endif #error #pragma #pragma STDGL invariant(all) //保证所有...

    自己创建不同规则的按钮

    #pragma once #endif // _MSC_VER >= 1000 #ifndef __AFXWIN_H__ #error include 'stdafx.h' before including this file for PCH #endif #include "resource.h" // main symbols ////////////////////////////...

    数字水印的(LSB算法)嵌入和提取VC++实现

    #pragma once #endif // _MSC_VER > 1000 #ifndef __AFXWIN_H__ #error include 'stdafx.h' before including this file for PCH #endif #include "resource.h" // main symbols //////////////////////////////...

    Visual C++课程设计案例精编(第二版) --第三章 系统进程管理器的开发

    #pragma once #endif // _MSC_VER > 1000 #ifndef __AFXWIN_H__ #error include 'stdafx.h' before including this file for PCH #endif #include "resource.h" // main symbols /////////////////////////////...

Global site tag (gtag.js) - Google Analytics