The use of static keyword summary

To interview, review the details of c ++ language, only to find himself still a lot of difference.

static int i1=1;
static int i2=1;
int i3;
static int i4;
int main()
{
    static int i5=1;
    int i6=1;
    int i7;
    cout<<&i1<<" "<<i1<<endl;
    cout<<&i2<<" "<<i2<<endl;
    cout<<&i3<<" "<<i3<<endl;
    cout<<&i4<<" "<<i4<<endl;
    cout<<&i5<<" "<<i5<<endl;
    cout<<&i6<<" "<<i6<<endl;
    cout<<&i7<<" "<<i7<<endl;
    getchar();
}

Output:

 

 

 First, that value:

Normal / static global variable (I3) is initialized to 0 by default, local variables common initial value is undefined (i7)

static local variable that is static local variable, will be initialized to 0

Besides memory storage location:

First put a memory map of the process:

 

 

Which is an uninitialized static initialization area + area. Note that the static region and constant region is not the same, such as constants: int a = 1; 1. Or char * x = "123"; the "123."

 

 This corresponds to two pictures of view is very clear

 

 

Well, enter the following themes:

Global variables are in a static variable area, whether or not static

Ordinary local variables on the stack area / heap, static local variable in a static variable area.

 

static different uses:

1.static global variables / functions, because the function must be global, so maybe with a pinch said.

Only one purpose: to prevent the use of other cpp own that limit the scope for cpp resides . Without static, extern can be modified and then the other cpp references.

So a.cpp suppose there is a function f.

If we want to let other cpp f refer to this function. So we should f a.cpp statement added to the header file and do not add static declaration, this will cause each cpp contains ah have a copy of the function f, greatly increasing the efficiency of the compiler! Of course, this issue back to write another blog post on the first go beyond that. In addition Never variables defined in the header file!

If we do not want to let other cpp reference function f, then we add static modification directly in front of the definition of f, so that other cpp not see the function f (even if the file contains a header is invisible).

2.static local variables, such as:

void f(){
    static int cnt=0;cnt++;
}

This function is called several times, cnt is equivalent to a few.

There is static local and global variables with static variable area, both only difference is: static local variable scope is limited to where the function, but does not release reaches beyond function, but will retain the existing values ​​continue to wait the next call.

static local variable is initialized only once! Cpp will not quit until release.

3.类中的static成员变量(类的静态成员变量):

在所有该类的实例(对象)中只存在一个副本,所有对象共享一个该静态变量。

类的静态成员变量需要在类中声明,类外定义,例子如下:

class A{
    static int x;
};
int A::x=1;

 

4.类中的static成员函数:

只能调用静态成员变量,别的和普通成员函数区别不大。

概括来说:类的静态成员函数、变量的存储位置不在类里,所以也就理所当然的没有this指针。所以静态成员函数无法调用普通成员变量也就合理了(因为没有this指针,根本找不到相应的变量位置!)。

由于二者都与类绑定,而不与特定对象绑定,所以二者可以除了可以通过对象访问外,还可以通过类名访问(A:: f();)。

 

Guess you like

Origin www.cnblogs.com/FdWzy/p/12424308.html