C++中的存储类
C++中的每个变量都有一个数据类型。当定义一个变量时,编译器会默认分配一个存储类。我们在C++中使用存储类来表达变量和方法的特性。它还指定了[变量和函数的范围、寿命和可见性]。
简介
这些特性使我们能够在程序的执行过程中跟踪变量的存在。本文将介绍各种存储类和显示它们如何工作的代码实例。
前提条件
要读完这篇文章,读者应该有。
- 安装了[Codeblocks IDE]。
- 对C++语言有一定的了解。
什么是生命周期和可见性?
一个变量被激活的时间长度被称为其生命周期。可见性指的是,根据设定的访问级别,变量可以访问代码的哪一部分。
存储类的类型
在C++编程语言中,通常有五种类型的存储类。
- 自动
- 注册
- 静态
- 外部的
- 可变的
下面的语法用于定义一个变量的存储类。
storage_class var_data_type var_name;
我们可以把代码分解成如下所示。
- storage_class - 它用于指定存储类。
- var_data_type - 它用于指定变量的数据类型。
- var_name - 它用于指定变量的名称。
让我们通过一些例子来看看这五个存储类。
1.自动类
它是所有局部变量的默认存储类。这些[局部变量是在一个函数或块内声明的]。在编写程序时,几乎不使用auto关键字。自动变量只能[在它们被声明的函数或块内]被访问。
它们不能在该函数或程序块之外被访问。我们也可以在嵌套块内访问它们,以及自动变量声明所在的父块。指针变量被用来[在其范围之外访问自动变量]。
我们需要指向内存中存储变量的同一区域。它的生命期与函数的生命期相同。当一个函数的执行完成后,该变量被销毁。默认情况下,每当声明时都会给它们一个垃圾值。
语法。
datatype var_name1 [= value]; // by default if you don’t use auto keyword
或
auto datatype var_name1 [= value];
示例显示自动存储类。
#include <iostream>
using namespace std;
void autoStorageClass()
{
cout << "Demonstration of auto storage class\n";
// Declare an auto variable
// We do not need data-type declaratiion
auto a = 50;
auto b = 5.2;
auto c = "AutoStorageClass";
auto d = 'A';
// Displaying the auto variables
cout << a << " \n";
cout << b << " \n";
cout << c << " \n";
cout << d << " \n";
}
int main()
{
// Demonstrating auto Storage Class
autoStorageClass();
return 0;
}
继续运行这里的代码。
输出。
Demonstration of auto storage class
50
5.2
A
2.寄存器
我们使用寄存器存储类来声明寄存器变量。寄存器变量的功能类似于自动变量。唯一的例外是,[如果有可用的寄存器,编译器会尝试将这些变量存储在微处理器的寄存器中]。
如果没有空闲的寄存器,它们就被完全存储在内存中。这使得在运行期间对寄存器变量的操作比存储在内存中的其他变量快得多。
一些在程序中需要定期访问的变量通常被声明在寄存器存储类中。这有助于提高程序的执行速度。一个寄存器变量的地址不能通过指针找到。该变量的最大尺寸等于寄存器的最大尺寸。我们不能使用'&'运算符,因为没有适合它的内存位置。
语法。
register datatype var_name1 [= value];
示例显示了寄存器存储类的用法。
#include <iostream>
using namespace std;
void registerStorageClass()
{
cout << "Demonstration of register storage class\n";
// declaration of a register variable
register char c = 'G';
//Lets dispkay the register variable
cout << "Value of 'c' which is"
<< " declared as register: " << c;
}
int main()
{
// Demonstrating the Storage Class
registerStorageClass();
return 0;
}
继续并运行这里的代码。
输出。
Demonstration of register storage class
Value of c which is declared as register: G
3.静态变量
静态变量是用这个存储类来声明的。静态变量[即使在其范围之外]也会保持[其值]。它们被初始化一次,一直存在到程序终止。静态变量的内存只被分配一次,并且不会再分配更多的内存,因为它不会被重新声明。
我们可以在代码的任何地方访问全局静态变量。编译器默认给它们赋值为0。在C++中,当我们在一个类的数据成员上使用静态时,[该成员]只有一个[副本被该类中的所有对象共享]。
语法。
static datatype var_name1 [= value];
例子显示静态存储类。
#include <iostream>
using namespace std;
// Function that contains static variables
// memory is retained during execution
int staticFun()
{
cout << "Static variables: ";
static int count = 0;
count++;
return count;
}
// Function that contains non-static variables
// Destroying memory
int nonStaticFun()
{
cout << "Non-Static variables: ";
int count = 0;
count++;
return count;
}
int main()
{
// Printing the static variables parts
cout << staticFun() << "\n";
cout << staticFun() << "\n";
;
// Printing the non-static variables parts
cout << nonStaticFun() << "\n";
;
cout << nonStaticFun() << "\n";
;
return 0;
}
继续运行这里的代码。
输出。
static variables: 1
static variables: 2
Non-Static variables: 1
Non-Static variables: 1
4.外部
当我们希望变量在多个文件中共享时,我们需要这个存储类。外部变量有一个全局范围,可以在声明它们的文件之外看到。整个程序都可以看到它,当同一个变量或函数被两个或多个文件共享时,就可以利用它。
外部变量的寿命与[声明它们的程序的寿命]相同。一个普通的全局变量也可以[通过在任何函数或程序块中的声明或描述前使用 "extern "关键字]而变成外部变量。
当你使用'extern'时,该变量不能被初始化,因为它所做的只是[将变量名指向一个先前确定的存储地址]。
语法。
extern datatype var_name1;
示例显示外部存储类。
#include <iostream>
using namespace std;
// Declaring the variable which is to
// be made extern an initial value can
// also be initialized to m
int m;
void externStorageClass()
{
cout << "Demonstrating extern class\n";
// We tell compiler that variable
// m is an extern variable. It's been defined somewhere else (above the main function)
extern int m;
// printing the extern variables
cout << "Value of 'm'which is "
<< "declared as extern: " << m << "\n";
// value of 'm' modified
m = 5;
// printing the modified values of
// extern variables
cout
<< "Modified value of 'm'"
<< " declared as extern: \n"
<< m;
}
int main()
{
// Demonstration of extern Storage Class
externStorageClass();
return 0;
}
输出。
Demonstration of extern class
Value of ‘m’ which is declared as extern: 0
Modified value of ‘m’ declared as extern: 5
5.可变的
只有类的对象才有mutable指定器。这使得一个对象的一个成员可以覆盖一个const成员函数。也就是说,一个常量成员函数可以改变一个可变成员。
即使你不希望该函数更新类/结构的其他成员,你可能需要改变一个或多个数据成员。这可以通过const函数来完成。mutable关键字使这一任务得以简单完成。
语法。
mutable datatype var_name1 [= value];
示例显示可变存储类。
#include <iostream>
using std::cout;
class Test {
public:
int m;
// Defining mutable variable n
// which can be modified
mutable int n;
Test()
{
m = 5;
n = 20;
}
};
int main()
{
// Our t1 is set to constant
const Test t1;
// We are trying to change the value
t1.n = 50;
cout << t1.n;
// Not commenting below the lines
// will throw errors
// t1.m = 8;
// cout << t1.m;
return 0;
}
输出。
50
结论
在这篇文章中,我们讨论了在C++程序中可以作为存储类的不同方法,以及它们是什么和它们的作用。
有了这些,你应该能够创建使用存储类来表达变量和方法的属性的程序。我希望你觉得这篇文章很直观,对你未来的程序很有用。