静态函数

 

静态函数只在所定义的.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()

原文地址:https://www.cnblogs.com/junnyfeng/p/192011.html