#define的作用域/作用范围

By Minidxer | October 16, 2007

#define可谓是从小用到大……(汗,夸张了点……),不过一直没有去考虑过它的作用范围,随手写了个程序测试了一下,结果是作用域的概念同样可以适用于#define,还是用代码说话吧。


用VC创建一个工程,加入下面文件

//class1.cpp

#include “stdafx.h”

#ifdef DEF_TEST_DEFINE_00
#pragma message(”DEF_TEST_DEFINE_00 was defined! Found in class1.cpp!”)
#endif

#ifdef DEF_TEST_DEFINE_11
#pragma message(”DEF_TEST_DEFINE_11 was defined(before)! Found in class1.cpp!”)
#endif

static int onlyTest(void)
{
#define DEF_TEST_DEFINE_11

return 0;
}

#ifdef DEF_TEST_DEFINE_11
#pragma message(”DEF_TEST_DEFINE_11 was defined(after)! Found in class1.cpp!”)
#endif

//end of class1.cpp

//class2.cpp
#include “stdafx.h”

#define DEF_TEST_DEFINE_00
#ifdef DEF_TEST_DEFINE_00
#pragma message(”DEF_TEST_DEFINE_00 was defined!Found in class2.cpp”)
#endif
int main(void){return 0;}

编译~

输出结果:

Compiling…
StdAfx.cpp
Compiling…
bitTest.cpp
class1.cpp
class2.cpp
DEF_TEST_DEFINE_11 was defined(after)! Found in class1.cpp!
Generating Code…
DEF_TEST_DEFINE_00 was defined!Found in class2.cpp
Linking…

defineTest.exe - 0 error(s), 0 warning(s)

仔细看一下,明白了吧?

Topics: 程序开发相关 | Tags: , , , ,

Search Posts

Archives

Sponsored Ads