Extern static and inline

Source: Internet
Author: User

I just started to learn to write some C ++ programs. I don't understand many problems. When you look at the big bull program, you can't keep up with your ideas. Many things need to accumulate slowly. Therefore, write down some knowledge points for future review.

This article from: http://blog.csdn.net/adriano119/article/details/2991317

-------------------------------- Static --------------------------------
Distribution in memory of a complete program:
============
| Code area |
------------------
| Global data zone |
------------------
| Heap area |
-----------------
| Stack area |
============
In general, dynamic data generated by new is stored in the heap zone, automatic variables in the function are stored in the stack zone, and global variables and static variables are stored in the global data zone.

Static has the following functions:
1. Extended survival; (process-oriented: Local)
2. restricted scope; (process-oriented: Global)
3. uniqueness. (Object-Oriented)

Static:

I. Static in process-oriented design
1. [Static global variable] // Add the keyword static before the global variable to define the variable as a static global variable.

Static global variables have the following features:
1) The variable allocates memory in the global data zone;
2) The uninitialized static global variable will be automatically initialized to 0 by the program (the value of the automatic variable is random unless it is explicitly initialized );
3) Static global variables are visible in the entire declared file, while extern outside the file is invisible;
You can share variables in files by defining global variables, but defining static global variables has the following benefits:
1) Static global variables cannot be used by other files;
2) variables with the same name can be defined in other files without conflict;

2. [Static local variable] Add the keyword static before the local variable, which is defined as a static local variable.

Generally, a variable is defined in the function body, and stack memory is allocated to the local variable whenever the program runs the statement. However, as the program exits from the function body, the system will reclaim the stack memory and the local variables will also become invalid. But sometimes we need to save the variable value between two calls. The general idea is to define a global variable for implementation. In this way, the variable no longer belongs to the function itself, and is no longer only controlled by the function, causing inconvenience to program maintenance.
Static local variables can solve this problem. Static local variables are stored in the global data zone, instead of in the stack. Each value is kept to the next call until the next value is assigned.
Static local variables have the following features:
1) The variable allocates memory in the global data zone;
2) Static local variables are initialized for the first time when the program executes the declaration of the object, that is, subsequent function calls will not be initialized;
3) Static local variables are generally initialized at the Declaration. If no Explicit initialization is performed, the static local variables will be automatically initialized to 0 by the program;
4) It always resides in the global data zone until the program is running. However, its scope is local scope. When the function or statement block that defines it ends, its scope ends;

3. Static Functions
Add the static keyword before the return type of the function. The function is defined as a static function. A static function is different from a common function. It can only be seen in the file where it is declared and cannot be used by other files.
Benefits of defining static functions:
1) static functions cannot be used by other files;
2) functions with the same name can be defined in other files without conflict;

2. Object-oriented static keywords (static keywords in the class)
1. static data member
Add the keyword static before the declaration of the data member in the class. The data member is the static data member in the class.
Static data members have the following features:
1) static data members are treated as class members. No matter how many objects are defined for this class, the static data member has only one copy in the program, which is shared by all objects of this type.
2) static data members are stored in the global data zone and all objects in this category are shared. Therefore, they do not belong to a specific class object and are visible in the scope when no class object is generated, that is, when no class instance is generated, we can operate on it;
Compared with global variables, static data members have two advantages:
1) The static data member does not enter the global namespace of the program, so there is no possibility of conflict with other global names in the program;
2) [hide information]. Static data members can be private members, but global variables cannot;

2. static member functions
It serves all the services of a class rather than the specific objects of a class. Compared with common functions, static member functions are not associated with any objects.(Shared, so it is unique. There is no entity dependency, so there is no this pointer, and there is no need)Therefore, it does not have the this pointer. In this sense, it cannot access non-static data members of class objects or non-static member functions. It can only call other static member functions.
Static member functions can be summarized as follows:
1) The function definition that appears in the class in vitro cannot specify the keyword static; (because this is not treated as an inline function at this time, see the header file in C ++.)
2) Static members can access each other, including static member functions accessing static data members and accessing static member functions;
3) non-static member functions can access static member functions and static data members at will;
4) static member functions cannot access non-static member functions and non-static data members.

----------------------------------- Extern ----------------------------
Extern
1. Basic explanation
Extern can be placed before a variable or function to indicate that the definition of a variable or function is in another file, prompting the compiler to find its definition in other modules when it encounters this variable or function. This behavior tells the compiler the definition of the variable/function.Already existsSomewhere, let the compiler go to other modules.SearchIts definition.
In addition, extern can be used to specify links.

2. extern "C"
The use of extern "C" is mainly because the C ++ language implements polymorphism during compilation, function names and functions are combined to form another function name (in short, the compiled function name is different from the one you previously declared ), the concept of no polymorphism in C Language certainly does not have this strange name change problem. This is the problem. When you want to call the C function in C ++, because of the different names, it will not find the definition of the called function, therefore, an error occurs.
In order to solve the conflicts between C and C ++, extern "C' exoccurs '.

There are several examples.

Http://developer.51cto.com/art/201104/256820.htm

Add several questions:

1: extern and # include differences

First, # include indicates the entire content, and extern indicates the specific content. Second, # include indicates the definition and declaration, while extern indicates the definition and Declaration. Finally, extern cannot contain static members, but # include can.

2: The function is preceded by extern, indicating that the function is visible to external files, that is, external files can be directly called.

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.