风歌的blog

已搬家到 blog.imxjf.top
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

静态函数

Posted on 2005-07-13 11:50  风歌  阅读(578)  评论(1编辑  收藏  举报
 

静态函数只在所定义的.c文件域中有效。

 

例子:

// d.h

#ifndef D_H

#define D_H

 

static void test();

void test1();

#endif

 

//dp.c

#include 
<iostream.h>

#include 
"d.h"

 

void test()

{

       cout
<<"hi"<<endl;

}


 

void test1()

{

       test();

}


 

//mainp.c

#include 
<iostream>

#include 
"d.h"

using namespace std;

 

void main()

{

       test1();

}


 



这例子是能顺利编译通过。因为test1()在所在的.c中能看到test()的定义,因而能调用之。但没有了test1()函数,而在main中直接调用test()会编译错误,提示找不到test()的定义。一般静态函数的声明和定义都放在一个.h中,以便包含这个.h就就能使用。

#ifndef D_H
#define D_H
#include 
<iostream.h>

static void test()
{
    cout
<<"HI"<<endl;
}



#endif

  

这样,包含这个.h就能直接调用test()