static 函数和 一般的函数有什么区别?
能不能给些例子

解决方案 »

  1.   

    static
    static declaratorWhen modifying a variable, the static keyword specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to 0 unless another value is specified. When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared). In C++, when modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all the instances of the class. When modifying a member function in a class declaration, the static keyword specifies that the function accesses only static members.For related information, see auto, extern, and register.Example// Example of the static keyword
    static int i;         // Variable accessible only from this filestatic void func();   // Function accessible only from this fileint max_so_far( int curr )
    {
       static int biggest;    // Variable whose value is retained
                              //    between each function call
       if( curr > biggest )
          biggest = curr;   return biggest;
    }// C++ onlyclass SavingsAccount
    {
    public:
       static void setInterest( float newValue )  // Member function
          { currentRate = newValue; }             //    that accesses
                                                  //    only static
                                                  //    members
    private:
       char name[30];
       float total;
       static float currentRate;    // One copy of this member is
                                    //    shared among all instances
                                    //    of SavingsAccount
    };// Static data members must be initialized at file scope, even
    //    if private.
    float SavingsAccount::currentRate = 0.00154;
      

  2.   

    简单说,即使类没有实例化,STATIC也可以用
      

  3.   

    楼上说的没错
    即使类没有实例化,STATIC也可以存在
    不象类的一些函数,必须在此类有对象存在的时候才有实体
      

  4.   

    你说的一般的函数是指类的成员函数吧, 它们的第一个参数是THIS指针(隐含),所以能访问成员变量
    static 函数没有隐含的THIS指针,不能访问成员变量
      

  5.   

    成员函数和静态成员函数都是属于类的,知识普通的成员函数(或变量)是属于类实例的,delete类实例后就不可以访问属于类实例的那部分(即不能访问普通成员函数和变量),但仍然可以访问类中静态的那部分成员函数和成员变量!
      

  6.   

    1、静态全局变量(又称全局静态变量):(1)       静态全局变量的定义:在全局变量前加一个static,使该变量只在这个源文件中可用。 (2)全局变量与全局静态变量的区别:    (a)若程序由一个源文件构成时,全局变量与全局静态变量没有区别。     (b)若程序由多个源文件构成时,全局变量与全局静态变量不同:全局静态变量使得该变量成为定义该变量的源文件所独享,即:全局静态变量对组成该程序的其它源文件是无效的。 (3)静态全局变量的作用:(a)不必担心其它源文件使用相同变量名,彼此相互独立。(b)在某源文件中定义的静态全局变量不能被其他源文件使用或修改。例如:一个程序由两个源文件组成,其中在一个源文件中定义了“int n;”,在另一个源文件中定义了“static int n;”则程序给它们分别分配了不同的空间,两个值互不干扰。 例如:下面在file1.cpp中声明全局变量n,在file2.cpp中定义全局静态变量n。文件file1.cpp和file2.cpp单独编译都能通过,但连接时,file1.cpp中的变量n找不到定义,产生连接错误。// file1.cpp# include <iostream.h>void fn()extern int n;void main(){n=20;cout<<n<<endl;fn();}// file2.cpp# include <iostream.h>static int n;  // 默认初始化为0,注意此处定义的n 只能在file2.cpp中使用。void fn(){n++;cout<<n<<endl;} 2、静态函数:使某个函数只在一个源文件中有效,不能被其他源文件所用。定义:在函数前面加上static。说明:函数的声明和定义默认情况下在整个程序中是extern的。静态函数的效果:(1)它允其他源文件建立并使用同名的函数,而不相互冲突。(2)       声明为静态的函数不能被其他源文件所调用,因为它的名字不能得到。
      

  7.   

    c语言里的static函数如何讲呢?
      

  8.   

    c语言里的static函数如何讲呢?