分享

static函数

 terryecnu 2015-01-07

static修饰符是一个能够减少此类命名冲突的有用工具。例如,以下声明语句

static int a;

其含义与下面的语句相同

int a;

只不过,a的作用域限制在一个源文件内,对于其他源文件,a是不可见的。因此,如果若干个函数需要共享一组外部对象,可以将这些函数放到一个源文件中,把它们需要用到的对象也都在同一个源文件中以static修饰符声明。

static修饰符不仅适用于变量,也适用于函数。如果函数f需要调用另一个函数g,而且只有函数f需要调用函数g,我们可以把函数f与函数g都放到同一个源文件中,并且声明函数gstatic

static int

g(int x)

{

 

}

void f() {

 

b=g (a);

}

我们可以在多个源文件中定义同名的函数g,只要所有的函数g都被定义为static,或者仅仅只有其中一个函数g不是static o因此,为了避免可能出现的命名冲突,如果一个函数仅仅被同一个源文件中的其他函数调用,我们就应该声明该函数为static

 

static函数

在C语言编程中,static的一个作用是信息屏蔽!
比方说,你自己定义了一个文件 -- 该文件中有一系列的函数以及变量的声明和定义!
你希望该文件中的一些函数和变量只能被该文件中的函数使用,那么,你可以在该函数、变量的前面加上static,代表他们只能被当前文件中的函数使用!

而在C++中,用static来作为信息屏蔽就显得没有必要了!因为,C++有了信息屏蔽的利器 -- class机制!

类中的private属性的变量和函数就对外禁止访问!

然后是C/C++通用的函数作用域的static型的变量!其目的,也是为了信息的屏蔽!
int fun() {
   static int 1;
   a++;
}
在第一次进入这个函数的时候,变量a被初始化为1!并接着自增1!

以后每次进入该函数,a就不会被再次初始化了,仅进行自增1的操作!

在static发明前,要达到同样的功能,则只能使用全局变量:

int 1;

int fun() {
   a++;
}
那么,a的值就有可能被其他函数所改变!

最后,说说类中的static变量和函数。

这种存储属性的变量和函数是同一种类的不同实例之间通信的桥梁!
#include <iostream>
using namespace std;

class {
public:
    static int num;    //    统计创建了多少个实例
    () {num++};    //    每创建一个实例,就让num自增1

    //    返回通过构造函数所创建过的A类实例的数目
    static int how_many_instance() {
        return num;
    }
}

static A::num 0;    //    需要在类申明的外部单独初始化!

int main() {
    cout << A::how_many_instance() << endl;
    a, b, c, d;
    cout << A::how_many_instance() << endl;
    system("pause");
}

一般,在类内部,是通过static属性的函数,访问static属性的变量! 
Q. How does static local variable works in C/C++?
A: Local static variable is persistent between function calls. It is not supposed to be destructed at the end of function. 
See the following example. 
#include<iostream>
using namespace std;
class Apple 
public:
    Apple(int,int);
    void addSize(int m) {size += m;}
    void printApple() cout<<"The Apple is "<<size<<" and "<<type<<"."<<endl;}
    ~Apple();
  private:
    int size;
    int type;
};
Apple::Apple(int s,int t){ 
  size=s;
  type=t;
}
Apple::~Apple(){ 
  printApple();
}
void testStatic(){ 
  static Apple a2(20,6);
  a2.addSize(10);
  a2.printApple();
}
int main() {
  testStatic(); //The Apple is 30 and 6.
  testStatic(); //The Apple is 40 and 6.
  return 0;
}

You see!!! testSatic() has been called twice, and the addSize(10) effect accumulated.
Note: There is local static variable in Java. 
Q. What does static function mean in C? Can you give an example?
A: static function is only visible by the file itself.
If you include static function in header file, you much include its body to make the function usable. See the following example, sum1() is static, sum2 is not, notice the code differences. 
//=======
//aa.h
#ifndef AA_H
#define AA_H
int sum2(int a, int b);
static int sum1(int a, int b);
int sum1(int a, int b) 
  return a+sum2(b, 10);
}
#endif
//=======
//aa.c
#include "aa.h"
int sum2(int a, int b) 
  return a+b;
}
//=======
//test.c
#include "aa.h"
int main() {
  printf("%d\n", sum1(3,4)); //17
  printf("%d\n", sum2(9,4)); //13
}
//=======
Read more from here:

http://bobcat./javachina/faq/cpp_01.htm#cpp_static_ttl 
补充一点,在类中,static型的成员函数,由于是类所拥有的,而不是具体对象所有的,这一点对于windows的回调机制非常有用。
因为对于回调函数而言,windows不会借助任何对象去调用它,也就不会传递this指针,那么对于一般成员函数作为回调函数的后果,就是堆栈中有一个随机的变量会成为this指针,这当然会引发程序的崩溃。
而static函数,由于是整个类的,屏蔽了this指针。因此,如果成员函数作为回调函数,就应该用static去修饰它。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多