c++存储类

简介: c++存储类

在C++中,存储类(Storage Class)定义了变量/函数的作用域和生命周期。它们决定了数据在内存中的存储方式以及其在程序中的可见性和持久性。C++提供了四种主要的存储类:autoregisterstaticextern。然而,auto 是默认的存储类,因此在大多数情况下我们不需要显式指定它。接下来,我们将详细讲解这些存储类,并附上编程示例。

1. auto 存储类

auto 是C++中的默认存储类。在函数内部定义的变量,除非明确指定了其他存储类,否则都将被视为 auto 存储类。auto 变量在定义它们的块或函数执行完毕后自动销毁。

示例

#include <iostream> 
void myFunction() { 
// auto 是默认的存储类,下面的变量实际上是 auto 类型的 
int autoVariable = 10; 
std::cout << "Value of autoVariable inside function: " << autoVariable << std::endl; 
// 当函数返回时,autoVariable 被销毁 
} 
int main() { 
myFunction(); // 调用函数 
// autoVariable 在这里不可见,因为它只在 myFunction 中存在 
return 0; 
}

2. register 存储类

register 存储类用于建议编译器将局部变量的值存储在寄存器中,而不是RAM中。这样做可以提高访问速度,因为寄存器的访问速度通常比RAM快。然而,register 修饰符在现代编译器中的实际效果并不明显,因为编译器通常会根据自己的优化策略来决定是否将变量存储在寄存器中。

示例

#include <iostream> 
void myFunction() { 
register int registerVariable = 5; // 建议编译器将变量存储在寄存器中 
std::cout << "Value of registerVariable inside function: " << registerVariable << std::endl; 
} 
int main() { 
myFunction(); // 调用函数 
return 0; 
}

3. static 存储类

static 存储类用于定义在函数调用之间保持其值的局部变量,以及具有文件作用域的变量和函数。

局部静态变量示例

#include <iostream> 
void myFunction() { 
static int staticVariable = 0; // 局部静态变量,在函数调用之间保持其值 
staticVariable++; 
std::cout << "Value of staticVariable inside function: " << staticVariable << std::endl; 
} 
int main() { 
myFunction(); // 输出:Value of staticVariable inside function: 1 
myFunction(); // 输出:Value of staticVariable inside function: 2 
// ... 以此类推 
return 0; 
}

文件作用域静态变量示例

// file1.cpp 
#include <iostream> 
static int fileScopeStatic = 42; // 文件作用域静态变量,仅在此文件中可见 
void printValue() { 
std::cout << "Value of fileScopeStatic: " << fileScopeStatic << std::endl; 
} 
// file2.cpp(如果尝试在此文件中访问 fileScopeStatic,则会出现编译错误)

4. extern 存储类

extern 存储类用于声明在其他文件中定义的变量或函数。这允许我们在多个文件之间共享数据或函数。

示例

假设我们有两个文件,file1.cppfile2.cpp,以及一个头文件 shared.h

shared.h:

// shared.h 
#ifndef SHARED_H 
#define SHARED_H 
extern int sharedVariable; // 声明在其他文件中定义的变量 
#endif // SHARED_H

file1.cpp:

// file1.cpp 
#include "shared.h" 
int sharedVariable = 100; // 定义变量 
void printValue() { 
std::cout << "Value of sharedVariable in file1: " << sharedVariable << std::endl; 
}
// file2.cpp 
#include <iostream> 
#include "shared.h" 
void anotherFunction() { 
std::cout << "Value of sharedVariable in file2: " << sharedVariable << std::endl; 
} 
int main() { 
printValue(); // 输出:Value of sharedVariable in file1: 100 
anotherFunction(); // 输出:Value of sharedVariable in file2: 100 
return 0; 
}

file2.cpp:

总结

C++的存储类为程序员提供了控制变量/函数作用域和生命周期的机制。autoregisterstaticextern 是四种主要的存储类,它们在不同的场景下有不同的用途。auto 是默认的存储类,用于局部变量;register 用于建议编译器将变量存储在寄存器中;static 用于定义在函数调用之间保持其值的局部变量,以及具有文件作用域的变量和函数;extern 用于声明在其他文件中定义的变量或函数。通过合理使用这些存储类,我们可以编写更高效、更可维护的C++代码。

相关文章
|
1天前
|
C++
C++职工管理系统(类继承、文件、指针操作、中文乱码解决)
C++职工管理系统(类继承、文件、指针操作、中文乱码解决)
2 0
C++职工管理系统(类继承、文件、指针操作、中文乱码解决)
|
1天前
|
C++
C++类和对象3
C++类和对象
4 0
|
1天前
|
存储 编译器 C++
C++类和对象2
C++类和对象
7 0
|
1天前
|
C++
C++类和对象1
C++类和对象
13 0
|
2天前
|
设计模式 编译器 C++
【C++航海王:追寻罗杰的编程之路】特殊类的设计方式你知道哪些?
【C++航海王:追寻罗杰的编程之路】特殊类的设计方式你知道哪些?
4 0
|
2天前
|
存储 编译器 C语言
【C++航海王:追寻罗杰的编程之路】string类
【C++航海王:追寻罗杰的编程之路】string类
6 0
|
2天前
|
编译器 C++
【C++航海王:追寻罗杰的编程之路】类与对象你学会了吗?(下)
【C++航海王:追寻罗杰的编程之路】类与对象你学会了吗?(下)
5 0
|
2天前
|
存储 编译器 C++
【C++航海王:追寻罗杰的编程之路】类与对象你学会了吗?(中)
【C++航海王:追寻罗杰的编程之路】类与对象你学会了吗?(中)
4 0
|
2天前
|
存储 编译器 C语言
【C++航海王:追寻罗杰的编程之路】类与对象你学会了吗?(上)
【C++航海王:追寻罗杰的编程之路】类与对象你学会了吗?(上)
7 2
|
2天前
|
存储 编译器 C++
【C++ 初阶路】--- 类和对象(下)
【C++ 初阶路】--- 类和对象(下)
7 1