Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7190415
  • 博文数量: 510
  • 博客积分: 12019
  • 博客等级: 上将
  • 技术积分: 6836
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-01 16:46
文章分类

全部博文(510)

文章存档

2022年(2)

2021年(6)

2020年(59)

2019年(4)

2018年(10)

2017年(5)

2016年(2)

2015年(4)

2014年(4)

2013年(16)

2012年(47)

2011年(65)

2010年(46)

2009年(34)

2008年(52)

2007年(52)

2006年(80)

2005年(22)

分类: C/C++

2010-10-14 18:13:34

编译时候,头文件会添加到源文件中。所以头文件中定义static 变量, 实际上是在多个源文件内,都有一个该变量,是独立的(注意)。
工程中一般不要这样使用。

//a.h该文件内定义g_aa静态

#ifndef __A_H__
#define __A_H__
static int g_aa;
#endif

//aa.h

#ifndef __AA_H__
#define __AA_H__
void fun();
#endif

//aa.cpp

#include <stdio.h>
#include <stdlib.h>
#include "a.h"

void fun()
{
        g_aa =2;
        printf("addr=%x\n",&g_aa);
}

//bb.h

#ifndef __BB_H__
#define __BB_H__

void fun2();
#endif

//bb.cpp

#include <stdio.h>
#include <stdlib.h>
#include "a.h"

void fun2()
{
        g_aa =3;
        printf("22 addr=%x\n",&g_aa);
}

//main.cpp

#include <stdio.h>
#include <stdlib.h>
#include "aa.h"
#include "bb.h"
#include "a.h"
int main(int argc, char**argv)
{
        fun();
        fun2();
        return 0;
}


运行结果是多个地址,显然是独立的


阅读(4066) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-10-15 18:00:24

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com