zoukankan      html  css  js  c++  java
  • C/C++的static成员

    1.静态全局变量

    static声明的变量,可以作为静态全局变量,静态全局变量存储在系统的全局数据区,而非static变量如果是声明的变量存储在栈区,如果是通过new/malloc分配的则存储在堆区.关于堆和栈存储的区别,我会在后续文章跟进.
    因为存储在全局数据区,所以这个变量是全局的,看以下代码:

    001
    002
    003
    004
    005
    006
    007
    008
    009
    010
    011
    012
    013
    014
    #include <iostream>
    using namespace std;
    static int item;
    void test1()
    {
        item++;
    }
    int main()
    {
        item = 0;
        test1();
        cout<<item<<endl;
        return 0;
    }

    当然,其实上述变量直接改变成非static,就这份代码而言也不影响结果,实际上static变量还有一个重要的作用:

    • 它的作用域只是定义它的那个文件,在其它文件中使用extern关键字也无法访问
    • 即使在其它文件中再定义名字相同的变量也不会冲突
    • 普通全局变量可以通过extern关键字实现在其它文件中访问这个变量.

    2.静态局部变量

    函数或者类内部定义的静态局部变量,可用于当我们调用函数时某个变量需要之前调用同一函数的值时候使用,比如我们需要统计某个函数被调用过多少次,下面的代码输出每行分别为1,2,3:

    001
    002
    003
    004
    005
    006
    007
    008
    009
    010
    011
    012
    013
    014
    015
    #include <iostream>
    using namespace std;
    void foo()
    {
        static int item = 0;
        item++;
        cout<<item<<endl;
    }
    int main()
    {
        foo();
        foo();
        foo();
        return 0;
    }

    同样,当一个类实例化后,如果需要之前已经实例化后对某个变量操作后的结果,同样可以用这方法求类已被实例化多少次.
    这种情况的静态变量作用域为函数内部或者类内部.

    3.类的静态数据成员

    其实作为类的静态数据成员,与上面的函数局部静态成员非常相似,区别在于,如果属于public,那么可以在类尚未实例化的时候就直接访问这个数据成员.看如下代码:

    001
    002
    003
    004
    005
    006
    007
    008
    009
    010
    011
    012
    013
    014
    015
    016
    017
    018
    019
    020
    021
    022
    023
    #include <iostream>
    using namespace std;
    class wst{
    public:
        wst();
        static int cnt;
    };
    int wst::cnt = 0;
    wst::wst()
    {
        cnt++;
        cout<<cnt<<endl;
    }
    int main()
    {
        //未实例化对象即可访问
        cout<<wst::cnt<<endl;
        wst t1;
        wst t2;
        wst t3;
        cout<<wst::cnt<<endl;
        return 0;
    }

    输出为:
    0
    1
    2
    3
    3
    相信已经非常明白了.

    4.静态函数和类的静态成员函数

    C/C++中普通的静态函数的作用是限制函数作用域只在本文件,而在其它文件中无法访问.
    对于类的静态成员函数:

    • 属于类,而不是属于对象,所以没有this指针,不能作为虚函数
    • 原因同上,所以可以在未实例化时调用静态函数
    • 因为没有this指针,只能访问静态成员函数和静态数据成员

    5.Things to keep in mind while using static member functions

    • A static member function can only access static member data, static member functions and data and functions outside the class.
    • You must take note not to use static member function in the same manner as non-static member function, as non-static member function can access all of the above including the static data member.
    • A non-static member function can be declared as virtual but care must be taken not to declare a static member function as virtual.
    • The programmer must first understand the concept of static data while learning the context of static functions. It is possible to declare a data member of a class as static irrespective of it being a public or a private type in class definition. If a data is declared as static, then the static data is created and initialized only once. Non-static data members are created again and again. For each separate object of the class, the static data is created and initialized only once. As in the concept of static data, all objects of the class in static functions share the variables. This applies to all objects of the class.
    • A non-static member function can be called only after instantiating the class as an object. This is not the case with static member functions. A static member function can be called, even when a class is not instantiated.
    • A static member function cannot have access to the ‘this’ pointer of the class.
  • 相关阅读:
    326. Power of Three
    python实现Excel删除特定行、拷贝指定行操作
    283. Move Zeroes
    268. Missing Number
    263. Ugly Number
    258. Add Digits
    257. Binary Tree Paths
    二叉树的创建,递归前序、中序、后序遍历以及队列实现层遍历
    242. Valid Anagram
    237. Delete Node in a Linked List
  • 原文地址:https://www.cnblogs.com/shihao/p/2325589.html
Copyright © 2011-2022 走看看