Elaiza's Bookshop

Learning C++ [1]

字数统计: 751阅读时长: 3 min
2020/07/06 Share

Header files in C++

头文件的存在使得函数、结构体等的调用变得简单,仅需在头文件中声明,而具体的实现在源文件中,从而方便了大量程序的多文件管理。一般而言,头文件的内容主要为以下六项:

  • 函数原型(function prototype)
  • 符号常量(#define 或者const定义的变量)
  • 结构体的声明(structure declaration)
  • 类的声明(class declaration)
  • 模板的声明(template declaration)
  • 内联函数(inline function)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// header file - demo1.h
// function prototype
#include <string>
using std::string;

void print_person(const person & p);
// constant
const int NUM_OF_PEOPLE = 60;
// structure declaration
struct person {
long id;
string name;
double gpa;
}
// class declaration
class debt {
private:
person p;
double amount;
int days;
public:
void calculate_debts(const person & p, double amount, int days);
}
// template declaration
template <typename T1, typename T2>
auto add(T1 t1, T2 t2) -> decltype(t1 + t2) {
return t1 + t2;
}
// inline function
inline plus(int n) { return n + 1; }

Storage Duration, Scope and Linkage

关于存储期(storage duration)

存储期可以简单的认为是C++中变量的生存期,C++11下有4中存储周期,分别为

  1. Automatic storage duration
  2. Static storage duration
  3. Thread storage duration
  4. Dynamic storage duration

关于作用域和连接(scope & linkage)

作用域(scope)描述了一个文件中一个名字(变量名、函数名等)的作用范围,对于变量而言,可以有local、global、class、namespace四种作用域,对于函数而言,只有global、class、namespace三种作用域。

连接(linkage)描述了对象和函数在编译单元之间共享的方式,其有三种模式。

连接方式 可见性
external linkage 在其他编译单元均可见
internal linkage 仅在本编译单元可见
no linkage 仅在作用域中可见

变量存储的类型

Storage Desc Duration Scope Linkage How Declared
Automatic Automatic Block None In a block
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 all functions
Static with internal linkage Static File Internal Outside all functions with the keyword static
1
2
3
4
5
6
7
// different type of static variables - static_type.cpp
double flow1 = 55.005; // static with external linkage
static double flow2 = 45.156; // static with internal linkage
double get_flow() {
static double flow3 = 52.5165; // static with no linkage
// ...
}

Specifiers and Qualifiers

Type Keyword
storage class specifer auto
register
static
extern
thread_local
mutable
cv-qualifier const
violate

Namespaces

由于越来越多的代码和第三方库的出现,变量、函数的命名有极大的可能出现冲突,因此C++引入了命名空间(namespace)来划分代码,namespace,结合::可以实现不同命名空间内变量和函数的访问。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// namespace.h
#include <string>
#include <iostream>

int NUM_OF_PEOPLE;

namespace person {
int NUM_OF_PEOPLE;

struct Person {
std::string name;
long id;
double gpa;
Person() { }
Person(const std::string & name, long id, double gpa): name(name), id(id), gpa(gpa) {
this->name = name;
this->id = id;
this->gpa = gpa;
}
};

void show_person(const Person & p) {
std::cout << "name: " << p.name << std::endl;
std::cout << "id: " << p.id << std::endl;
std::cout << "gpa: " << p.gpa << std::endl;
}
}

namespace debts {
using namespace person;
struct Debt {
Person person;
double amount;
};
}

int main() {
int NUM_OF_PEOPLE;
cin >> NUM_OF_PEOPLE;
cin >> ::NUM_OF_PEOPLE;
cin >> person::NUM_OF_PEOPLE;
}
CATALOG
  1. 1. Header files in C++
  2. 2. Storage Duration, Scope and Linkage
    1. 2.1. 关于存储期(storage duration)
    2. 2.2. 关于作用域和连接(scope & linkage)
    3. 2.3. 变量存储的类型
    4. 2.4. Specifiers and Qualifiers
  3. 3. Namespaces