第九章 内存模型和名称空间
9.1 单独编译
可以把原程序分为3个部分
-
A header file that contains the structure declarations and prototypes for functions using those structures --coordin.h
通过使用#ifndef...
#endif来避免头文件重复包含,实际工作是 make it ignore the contents of all but the first inclusion
-
A source code file that contains the code for the structure-related functions--file2.cpp
-
A source code file that contains the code that calls upon those functions--file1.cpp
9.2 存储持续,作用域和链接性(Storage Duration,Scope,and Linkage
-
Variables declared inside a function definition have automatic storage duration; that includes function parameters. They are created when program execution enters the function or block in which they are defined, and the memory used for them is freed when execution leaves the function or block. C++ has two kinds of automatic storage duration variables.
-
Variables defined outside of a function definition or else by using the keyword static have static storage duration. They persist for the entire time a program is running. C++ has three kinds of static storage duration variables.
-
Memory allocated by the new operator persists until freed with the delete operator or until the program ends, whichever comes first. This memory has dynamic storage duration and sometimes is termed the free store.
9.2.1作用域和链接
作用域(scope)描述了名称在文件(翻译单元)的多大范围内可见。
链接性(linkage)描述了名称如何在不同单元内共享。
9.2.2自动存储持续
9.2.3静态持续变量
Storage Description | Duration | Scope | Linkage | How Declared |
---|---|---|---|---|
automatic | automatic | block | none | in a block (optionally with the keyword auto) |
register | automatic | block | none | in a block with the keyword register |
static with no linkage | static | block | none | in a block with the keyword static |
static with external linkage | static | file | external | outside of all functions |
static with internal linkage | static | file | internal | outside of all functions with the keyword static |
全局还是局部?
通常情况下,应使用局部变量,应在需要知晓时才传递数据,而不应不加区分地使用全局变量来使数据可用。
2。静态持续,内部链接性
只有一个文件包含了该变量的外部定义,其他文件要使用该变量,必须在引用声明中使用关键字extern
REMEMBER:
In a multifile program, you can define an external variable in one and only one file. All other files using that variable have to declare that variable with the extern keyword 否则使用static来隐藏外部变量。
9.2.4说明符和限定符
The keyword static, when used with a file-scope declaration, indicates internal linkage. When used with a local declaration, it indicates static storage duration for a local variable.
The keyword extern indicates a reference declaration, that is, that the declaration refers to a variable defined elsewhere
9.2.5 函数和链接性
You also can use the keyword static to give a function internal linkage, confining its use to a single file. You would apply this keyword to the prototype and to the function definition
9.2.6语言链接
extern "C" void spiff(int); // use C protocol for name look-up extern void spoff(int); // use C++ protocol for name look-up extern "C++" void spaff(int); // use C++ protocol for name look-up
The first uses C language linkage. The second and third use C++ language linkage. The second does so by default, and the third explicitly.
9.2.7存储方案和动态分配9.3 名称空间
You might want the List class from one library and the Tree from the other, and each might expect its own version of Node. Such conflicts are termed namespace problems.