The lifetime of the object inquiry

Object lifetime
sharing and data protection mechanisms is an important feature of C ++. Referred to the sharing of data, we need to analyze the survival of objects in C ++.
Survival, by definition, from birth to the end of the period.
The lifetime of the object is divided into static and dynamic lifetime of survival.
A static survival
when survival and run the program object of the same, said the object has a static lifetime.
In the role of target namespace declaration space is a static survival.
In the lifetime of an object with a static declaration in the local scope, use the keyword static.
Features of static variables local scope: will not die out with the end of two function calls; that is shared between each call to a static variable.
Second, the dynamic survival
range dynamic lifetime is more extensive, in addition to the two kinds of static objects have a dynamic lifetime of survival, also known as a local object lifetime.
Local born in the lifetime of the object declaration point and ends at block declaration occurs when finished.

To better understand the dynamic and static lifetime of survival, I used a relatively simple code so that we understand the characteristics of the specific survival.
...

#include<iostream>
using namespace std;
int i = 1;                              //i为全局变量,具有静态生存期
void other()
{
    static int a = 2;                   //a为静态局部变量,赋值初始化
    static int b;                       //b为静态局部变量,不赋值
    int c = 10;                         //c为局部变量,具有动态生存期
    a += 2;
    i += 32;
    c += 5;
    cout << "---OTHER---" << endl;
    cout << "i:" << i << "a:" << a << "b:" << b << "c:" << c << endl;
    b = a;
}

int main()
{
    static int a;
    int b = -10;
    int c = 0;
    cout << "--MAIN---" << endl;
    cout << "i:" << i << "a:" << a << "b:" << b << "c:" << c << endl;
    c += 8;
    other();
    cout << "--MAIN---" << endl;
    cout << "i:" << i << "a:" << a << "b:" << b << "c:" << c << endl;
    i += 10;
    other();
    return 0;
}

...
set the variable ideas
I set two variables with static period, but one of the assignment, another definition only, comparing the results of a and b, observe the changes
set aside a period of dynamic variables, a and c The results were compared.
The results shown in a run
Figure I
, as we know, every time recall other functions, a and b of the initial value does not change, when the main function is running, which do not have any static constant change, are the initial value and I found no assignment of a static constant b, the initial value is 0, so it is proved that
the initial value is not specified when defining the basic types of static variables, it will be given a zero initialization;
dynamic variables only play a role in local scope .
Third, examples of verification
believe have a basic understanding of the characteristics of static objects and objects of local survival, followed by a more complex example of a well understanding of object lifetime.
...

#include<iostream>
using namespace std;
class Clock
{
public:
    Clock();
    void setTime(int newH, int newM, int newS)
    {
        hour = newH;
        minute = newM;
        second = newS;
    }
    void showTime()
    {
        cout << hour << ":" << minute << ":" << second << endl;
    }

private:
    int hour, minute, second;
};
Clock::Clock() : hour(0),minute(0),second(0){}
Clock globCLOCK;
int main()
{
    cout << "First time output :" << endl;
    globCLOCK.showTime();
    globCLOCK.setTime(8, 30, 30);
    Clock myClock(globCLOCK);
    cout << "Second time output: " << endl;
    myClock.showTime();
    return 0;
}

...
operating results Figure II
Figure II
This is a clock program, more clearly reflects the characteristics of static and dynamic object lifetime.

Guess you like

Origin www.cnblogs.com/xlog/p/11587362.html