c ++键值存储工具类_C ++中的存储类

c ++键值存储工具类

Storage classes are used to specify the lifetime and scope of variables. How storage is allocated for variables and How variable is treated by complier depends on these storage classes.

存储类用于指定变量的生存期和范围。 如何为变量分配存储空间以及编译器如何处理变量取决于这些存储类。

These are basically divided into 5 different types:

这些基本上分为5种不同的类型:

  1. Global variables

    全局变量

  2. Local variables

    局部变量

  3. Register variables

    注册变量

  4. Static variables

    静态变量

  5. Extern variables

    外部变量

全局变量 (Global Variables)

These are defined at the starting , before all function bodies and are available throughout the program.

这些是在开始时定义的,在所有功能体之前,并且在整个程序中都可用。

using namespace std;
int globe;      // Global variable
void func();
int main()
{
    .....
}

局部变量 (Local variables)

They are defined and are available within a particular scope. They are also called Automatic variable because they come into being when scope is entered and automatically go away when the scope ends.

它们已定义并且在特定范围内可用。 它们也称为自动变量,因为它们是在输入作用域时产生的,而在作用域结束时自动消失的。

The keyword auto is used, but by default all local variables are auto, so we don't have to explicitly add keyword auto before variable dedaration. Default value of such variable is garbage.

使用了关键字auto ,但是默认情况下,所有局部变量都是auto,因此我们不必在变量替换之前显式添加关键字auto。 该变量的默认值是垃圾

注册变量 (Register variables)

This is also a type of local variable. This keyword is used to tell the compiler to make access to this variable as fast as possible. Variables are stored in registers to increase the access speed.

这也是局部变量的一种。 此关键字用于告诉编译器尽可能快地访问此变量。 变量存储在寄存器中以提高访问速度。

But you can never use or compute address of register variable and also , a register variable can be declared only within a block, that means, you cannot have global or static register variables.

但是,您永远不能使用或计算寄存器变量的地址,而且,只能在一个内声明寄存器变量,这意味着您不能具有全局静态寄存器变量

静态变量 (Static Variables)

Static variables are the variables which are initialized & allocated storage only once at the beginning of program execution, no matter how many times they are used and called in the program. A static variable retains its value until the end of program.

静态变量是在程序执行开始时仅初始化和分配一次存储的变量,无论在程序中使用和调用它们多少次。 静态变量将保留其值,直到程序结束。

void fun()
{
    static int i = 10;
    i++;
    cout << i;
}
int main()
{
    fun();      // Output = 11
    fun();      // Output = 12
    fun();      // Output = 13
}

As, i is static, hence it will retain its value through function calls, and is initialized only once at the beginning.

由于i是静态的,因此它将通过函数调用保留其值,并且在开始时仅初始化一次。

Static specifiers are also used in classes, but that we will learn later.

静态说明符也用于类中,但是稍后我们将学习。

外部变量 (Extern Variables)

This keyword is used to access variable in a file which is declared & defined in some other file, that is the existence of a global variable in one file is declared using extern keyword in another file.

此关键字用于访问在其他文件中声明和定义的文件中的变量,即在一个文件中使用extern关键字声明另一个文件中存在全局变量。

extern keyword in C++

翻译自: https://www.studytonight.com/cpp/storage-classes-in-cpp

c ++键值存储工具类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值