📜  C++中的存储类及其示例

📅  最后修改于: 2021-05-30 20:27:44             🧑  作者: Mango

存储类用于描述变量/函数。这些功能基本上包括范围,可见性和生存时间,它们可以帮助我们在程序运行时跟踪特定变量的存在。要为变量指定存储类,应遵循以下语法:

句法:

storage_class var_data_type var_name; 


C++使用5个存储类,即:

  1. 汽车
  2. 登记
  3. 外部
  4. 静止的
  5. 易变的

以下是每个存储类别的详细说明:

  • auto :auto关键字提供类型推断功能,使用该功能可以自动推断出编程语言中表达式的数据类型。这将花费更少的时间来写出编译器已经知道的内容。由于仅在编译器阶段推导所有类型,因此编译时间会稍微增加,但不会影响程序的运行时间。此功能还扩展到函数和非类型模板参数。从C++ 14开始,函数将根据其return语句推导返回类型。从C++ 17开始,对于非类型模板参数,将从参数推导类型。

例子:

C++
#include 
using namespace std;
 
void autoStorageClass()
{
 
    cout << "Demonstrating auto class\n";
 
    // Declaring an auto variable
    // No data-type declaration needed
    auto a = 32;
    auto b = 3.2;
    auto c = "GeeksforGeeks";
    auto d = 'G';
 
    // printing the auto variables
    cout << a << " \n";
    cout << b << " \n";
    cout << c << " \n";
    cout << d << " \n";
}
 
int main()
{
 
    // To demonstrate auto Storage Class
    autoStorageClass();
 
    return 0;
}


C++
#include 
using namespace std;
 
// declaring the variable which is to
// be made extern an intial value can
// also be initialized to x
int x;
void externStorageClass()
{
 
    cout << "Demonstrating extern class\n";
 
    // telling the compiler that the variable
    // x is an extern variable and has been
    // defined elsewhere (above the main
    // function)
    extern int x;
 
    // printing the extern variables 'x'
    cout << "Value of the variable 'x'"
         << "declared, as extern: " << x << "\n";
 
    // value of extern variable x modified
    x = 2;
 
    // printing the modified values of
    // extern variables 'x'
    cout
        << "Modified value of the variable 'x'"
        << " declared as extern: \n"
        << x;
}
 
int main()
{
 
    // To demonstrate extern Storage Class
    externStorageClass();
 
    return 0;
}


C++
#include 
using namespace std;
 
// Function containing static variables
// memory is retained during execution
int staticFun()
{
    cout << "For static variables: ";
    static int count = 0;
    count++;
    return count;
}
 
// Function containing non-static variables
// memory is destroyed
int nonStaticFun()
{
    cout << "For Non-Static variables: ";
 
    int count = 0;
    count++;
    return count;
}
 
int main()
{
 
    // Calling the static parts
    cout << staticFun() << "\n";
    cout << staticFun() << "\n";
    ;
 
    // Calling the non-static parts
 
    cout << nonStaticFun() << "\n";
    ;
    cout << nonStaticFun() << "\n";
    ;
    return 0;
}


C++
#include 
using namespace std;
 
void registerStorageClass()
{
 
    cout << "Demonstrating register class\n";
 
    // declaring a register variable
    register char b = 'G';
 
    // printing the register variable 'b'
    cout << "Value of the variable 'b'"
         << " declared as register: " << b;
}
int main()
{
 
    // To demonstrate register Storage Class
    registerStorageClass();
    return 0;
}


C++
#include 
using std::cout;
 
class Test {
public:
    int x;
 
    // defining mutable variable y
    // now this can be modified
    mutable int y;
 
    Test()
    {
        x = 4;
        y = 10;
    }
};
 
int main()
{
    // t1 is set to constant
    const Test t1;
 
    // trying to change the value
    t1.y = 20;
    cout << t1.y;
 
    // Uncommenting below lines
    // will throw error
    // t1.x = 8;
    // cout << t1.x;
    return 0;
}


输出:
Demonstrating auto class
32 
3.2 
GeeksforGeeks 
G





  • extern :extern存储类仅告诉我们该变量是在其他位置定义的,而不是在使用该变量的同一块中定义的。基本上,该值是在另一个块中分配给它的,也可以在另一个块中对其进行覆盖/更改。因此,extern变量不过是一个全局变量,该变量在声明有合法值的情况下进行了初始化,以便在其他地方使用。可以在任何函数/块中访问它。同样,也可以通过在任何函数/块的声明/定义之前将“ extern”关键字放置在普通全局变量中,将其设置为外部变量。这基本上表示我们不是在初始化新变量,而是仅在使用/访问全局变量。使用外部变量的主要目的是可以在属于大型程序的两个不同文件之间访问它们。有关外部变量如何工作的更多信息,请查看此链接。

例子:

C++

#include 
using namespace std;
 
// declaring the variable which is to
// be made extern an intial value can
// also be initialized to x
int x;
void externStorageClass()
{
 
    cout << "Demonstrating extern class\n";
 
    // telling the compiler that the variable
    // x is an extern variable and has been
    // defined elsewhere (above the main
    // function)
    extern int x;
 
    // printing the extern variables 'x'
    cout << "Value of the variable 'x'"
         << "declared, as extern: " << x << "\n";
 
    // value of extern variable x modified
    x = 2;
 
    // printing the modified values of
    // extern variables 'x'
    cout
        << "Modified value of the variable 'x'"
        << " declared as extern: \n"
        << x;
}
 
int main()
{
 
    // To demonstrate extern Storage Class
    externStorageClass();
 
    return 0;
}
输出:
Demonstrating extern class
Value of the variable 'x'declared, as extern: 0
Modified value of the variable 'x' declared as extern: 
2





  • static :此存储类用于声明静态变量,这些静态变量在用C++语言编写程序时经常使用。静态变量具有保留其值的属性,即使它们超出其范围也是如此!因此,静态变量在其范围内保留了其最后一次使用的值。因此,我们可以说它们仅被初始化一次,并且一直存在到程序终止为止。因此,没有分配新的内存,因为它们没有被重新声明。它们的范围对于定义它们的函数是局部的。全局静态变量可以在程序中的任何位置访问。默认情况下,编译器将它们分配为值0。

C++

#include 
using namespace std;
 
// Function containing static variables
// memory is retained during execution
int staticFun()
{
    cout << "For static variables: ";
    static int count = 0;
    count++;
    return count;
}
 
// Function containing non-static variables
// memory is destroyed
int nonStaticFun()
{
    cout << "For Non-Static variables: ";
 
    int count = 0;
    count++;
    return count;
}
 
int main()
{
 
    // Calling the static parts
    cout << staticFun() << "\n";
    cout << staticFun() << "\n";
    ;
 
    // Calling the non-static parts
 
    cout << nonStaticFun() << "\n";
    ;
    cout << nonStaticFun() << "\n";
    ;
    return 0;
}
输出:
For static variables: 1
For static variables: 2
For Non-Static variables: 1
For Non-Static variables: 1





  • register :此存储类声明与自动变量具有相同功能的寄存器变量。唯一的区别是,如果有可用寄存器,则编译器将尝试将这些变量存储在微处理器的寄存器中。这使得寄存器变量的使用比程序运行期间存储在存储器中的变量的使用要快得多。如果没有可用的寄存器,则将它们仅存储在存储器中。通常,使用register关键字声明一些在程序中经常访问的变量,这可以缩短程序的运行时间。这里要注意的重要和有趣的一点是,我们无法使用指针获取寄存器变量的地址。

例子:

C++

#include 
using namespace std;
 
void registerStorageClass()
{
 
    cout << "Demonstrating register class\n";
 
    // declaring a register variable
    register char b = 'G';
 
    // printing the register variable 'b'
    cout << "Value of the variable 'b'"
         << " declared as register: " << b;
}
int main()
{
 
    // To demonstrate register Storage Class
    registerStorageClass();
    return 0;
}
输出:
Demonstrating register class
Value of the variable 'b' declared as register: G





  • 可变的:有时您需要通过const函数来修改一个或多个class / struct的数据成员,即使您不希望该函数更新class / struct的其他成员也是如此。使用mutable关键字可以轻松地执行此任务。关键字mutable主要用于允许修改const对象的特定数据成员。当我们将一个函数声明为const时,传递给该函数的指针将变为const。将可变添加到变量允许const指针更改成员。

例子:

C++

#include 
using std::cout;
 
class Test {
public:
    int x;
 
    // defining mutable variable y
    // now this can be modified
    mutable int y;
 
    Test()
    {
        x = 4;
        y = 10;
    }
};
 
int main()
{
    // t1 is set to constant
    const Test t1;
 
    // trying to change the value
    t1.y = 20;
    cout << t1.y;
 
    // Uncommenting below lines
    // will throw error
    // t1.x = 8;
    // cout << t1.x;
    return 0;
}
输出:
20





要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”