#ifndef #define #endif
来源:互联网 发布:淘宝美工用的什么软件 编辑:程序博客网 时间:2024/06/01 08:24
在做程设作业的过程中发现标答都会有一句#ifndef XXX 和 #endif,是什么意思呢?
这是#——预处理运算符的三种用法之一:条件编译
#的用法:
1、包含文件,如#include<stdio.h>
2、宏定义,如#define max_length 100
3、条件编译,如#ifndef、#endif
条件编译是什么意思呢
ifndef其实是if not define的缩写,如果没有定义,那就定义,直到endif
看一个例子:
#include <iostream>using namespace std;#ifndef max_length#define max_length 50#endif#ifndef max_length#define max_length 100#endifint main() { cout << max_length << endl; return 0;}那么输出结果显然是50了,由于max_length已经定义为50了,定义为100的部分没有执行。
当然这个用法还有避免重复包含头文件的作用,看这个例子
test_main.cpp:
#include <iostream>using namespace std;#include "test1.h"#include "test.h"int main() { test t; return 0;}test.h:
#include <iostream>using namespace std;#ifndef _TEST_H#define _TEST_Hclass test { public: test() { cout << "Test has been constructed." << endl; }};#endiftest1.h:
#include <iostream>using namespace std;#ifndef _TEST_H#define _TEST_Hclass test { public: test() { cout << "Test 1 has been constructed." << endl; }};#endif注意在test_main中,我们先#include "test1.h",那么在下一个#include "test.h"中由于已经定义了_TEST_H标识,就不会再定义test.h里面的test类,注意一般标识的写法是下划线+头文件名大写+下划线+H,如_TEST_H,那么输出显然是:
假如调换#include "test1.h"和#include "test.h"的顺序:
还有一个有趣的现象,在因为已经定义而不会被编译的头文件里,就算有错误也没事,这是因为根本没被编译
注意下面是先#include "test.h"的
但是乱打的错误必须在标识内部:
还有一些奇怪的东西:
1 0
- #ifndef、#define、#endif
- #ifndef/#define/#endif 小结
- #ifndef #define #endif用法
- 有关#ifndef #define #endif
- #ifndef,#define #endif作用
- #ifndef#define#endif用法
- #ifndef #define #endif
- #define #ifndef #endif
- ifndef/define/endif
- #ifndef /#define/#endif
- #ifndef、#define、#endif
- #ifndef#define#endif
- #ifndef, #define, #endif 作用
- ifndef define endif
- #ifndef #define #endif作用
- #ifndef, #define, #endif 作用
- #ifndef #define #endif
- ifndef/define/endif
- 接入微信分享 以及使用eclipse调试cocos2dx
- 基于JMS消息中间件的分布式系统初探究(一) - 通过JMS实现Web服务器与服务框架的通讯
- java 多线程学习笔记之 线程中断
- 我国互联网公司在美上市热潮消退
- 大根堆优先队列
- #ifndef #define #endif
- 常用打jar包方法
- 生成唯一32位ID编码代码Java(GUID)
- rfid系统组成及其功能
- ubuntu下使用配置好的VIM神器
- Android Fragment完全解析,关于碎片你所需知道的一切
- 深入理解Java内存模型(一)——基础
- RFID基础知识要点
- 关于textView上显示placeholder