C + + on the static keyword charging---supplement (7) "Effective C + +" __c++

Source: Internet
Author: User

Finally, there is time, you can fill in the pit before digging.
What is the difference between the static in C + + and the C language? We instruct the C + + language to add OOP ideas, and with a new reference scenario for static in C, let's analyze the static properties of the C language section in C + + and the static properties of the C + + OOP section. C language part static:

1) Static local variable
The last static member value that is eventually returned when the static local variable is called in the function, which we call "phantom", should look like a previous value, but after the function executes, the static member variable value is always updated to the most recent value.

#include <iostream>
#include <string>
using namespace std;
int& hel (int t) {
    static int i = t + 1;
    return i;
}

int main () {
    int i = ten, j = m;
    cout << Hel (i) << "" << Hel (j) << Endl;
    if (hel (i) = = Hel (j)) {
        cout << "true" << Endl;
    }
    else{
        cout << "false" << Endl;
    }
    return 0;
}

The results of the operation are:

2 static global variable

It can be found that when a static global variable is invoked, there is a corresponding logic and storage, so there is no behavior that always returns the current value like a static local variable.

#include <iostream>
#include <string>
using namespace std;
static int i = ten;
int Hel () {
    i++;
    return i;
}

int main () {
    cout << i << Endl;
    cout << Hel () << Endl; 
    cout << Hel () << Endl;
    if (hel () = = Hel ()) {
        cout << "true" << Endl;
    }
    else{
        cout << "false" << Endl;
    }
    return 0;
}

The results of the operation are:

3) static function
Static functions, like static member variables, have two properties: they cannot be referenced by other files, and other files can define functions with the same name;

Test1.cpp

#include <iostream>
using namespace std;
static void Hello () {
    cout << "Hello" << Endl;
}

Test.cpp

#include <iostream>
#include <string>
using namespace std;
extern void  hello ();
int main () {
    hello ();
    return 0;
}

We can find the error:
error LNK2019: unresolved external symbol "void __cdecl hello (? hello@ @YAXXZ), which is referenced in function _main
If we change to the following code.

Test1.cpp

#include <iostream>
using namespace std;
static void Hello () {
    cout << "Hello" << Endl;
}

Test.cpp

#include <iostream>
#include <string>
using namespace std;
extern void  hello ();
(static/non-static) void Hello () {
    cout << "hahaha" << Endl;
}
int main () {
    hello ();
    return 0;
}

The results of the operation are:
What about the features in C + + OOP.

1 static member variable
The static member variable belongs to the whole class, and the value has the initialization design outside the class, and can be called by the static member function or the ordinary member function;
2) static member function
Static member functions can only be modified for static member variables and cannot invoke ordinary member variables.

Come on, fellow friends, over the minefield, is Babel Avenue, dare to cross the threshold of half a step just a beginning, not afraid of thorns, the courage to study, every day is a great journey, refueling ...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.