最近在阅读thinking in c++,通过这个博客记录下学习中遇到的一些比较重要的知识点。现在看到了第10章,前面的几个章节之前阅读时没有这种记录的意识,等有时间,再重新翻看下,再记录吧。
第10章的Name Control主要是介绍了static和namespace如何控制变量的作用域。
Static elements from C
In both C and C++ the keyword static has two basic meanings: 包括C和C++,关键词static都有两个基本的意义:
1.Allocated once at a fixed address. 只在固定的地址上分配一次空间
2.Local to a particular translation unit(and local to a class scope in c++). Here, static controls the visibility of a name, so that name can't be seen outside the translation unit or class. 让变量本地到特定的编译单元中(也本地到C++的类作用域中)。这里,static控制了命名的可见性,因此命名不能给被编译单元或者类之外所见。
static variables inside functions
static class object inside functions
static object destructors
controlling linkage
Ordinarily,any name at file scope(that is ,not nested inside a class or functions) is visible throughout all translation units in a program. This is often called external linkage because at link time the name is visible to the linker everywhere, external to that translation unit.
通常,文件范围内的名称(也就是说,名称没有嵌套在类中或者函数中)贯穿程序的所有编译单元中都是可见的。这也经常被叫做外部连接,因为在连接时期,名称在连接器的任何一个地方是可见的,外部到编译单元。
An object or function name at file scope that is explicitly declared static is local to its translation unit. That name has internal linkage.This mean you can use the same name in other translation units without a name clash.
在文本范围内,一个用static声明的对象或函数名对于这个编译单元来说是本地的,那个变量有内部链接。这意味着你可以在其他的编译单元中使用相同的名称,而不用担心命名冲突。
One advantage to internal linkage is that the name can be placed in a header file without worrying that there will be a clash at link time.Name that are commonly place in header files,such as const definitions and inline functions,default to internal linkage.However,const defaults to internal linkage only in C++; in C it defaults to external linkage .
内部链接的一个好处是我们可以将一个命名放到头文件中而不用担心在连接时产生冲突。命名经常被放在头文件中,例如const 定义和内联函数,默认是内部连接。然而,const定义只有在C++中才默认为内部连接,在C中,默认是外部链接。
Namespace
Creating a namespace
- A namespace definition can appear only at global scope ,or nested within another namespace.
- No terminating semicolons is necessary after the closing brace of a namespace definition.
- A namespace definition can be "continued" over multiple header using a syntax that, for a class, would appear to be a redefinition.
//: C10:Header1.h
#ifndef HEADER1_H
#define HEADER1_H
namespace Mylib
{
extern int x;
void f();
}
#endif //HEADER1_H
//: C10:Header2.h
#ifndef HEADER2_H
#define HEADER2_H
#include "Header1.h"
//add more names in Mylib
namespace Mylib{ //
extern int y;
void g();
//...
}
#endif //HEADER2_H
//: C10:Continuation.cpp
#include "Header2.h"
int main(){}
- A namespace can be aliased to another name , so you don't have to type an unwieldy name created by a library vendor:
namespace BobsSuperDuperLibrary
{
class Widget{};
class Poppit{};
}
//Too much to type.then i can alias it
namespace Bog = BobsSuperDuperLibrary;
int main(){}
- You cannot create an instance of a namespace as you can with a class.
- 命名空间的定义只能出现在全局范围内或者嵌套在另外一个命名空间中。
- 命名空间定义完后的右大括号后不必有分号。
- 一个命名空间可以在多个头文件中连续定义,使用这语法,对于class来说,将会出现重复定义。
- 一个命名空间可以用别的名字作为别名,所以你就不必输入库供应商创建的笨重的名称。
- 你不能为一个命名空间创建实例,就像你对class做的那样。