Storage class in C / C ++

The Storage Class defines the scope and life of variables and / or functions within a C / C ++ program. They often precede the type of data they impact. Here are the storage classes that can be used in C / C ++:

auto

register

static

extern

mutable

Storage class is auto in C / C ++

The storage class auto in C / C ++ is the default storage class for all local variables in C / C ++:

 { int diemthi ; auto int diemthi ; } 

The example above defines two variables with the same storage class, auto can only be used inside welding, for example, for internal variables.

The class stores the register in C / C ++

The register class in C / C ++ is used to define local variables that should be stored in a register instead of RAM. That is, the variable has a maximum size equal to the register size (usually 1 word) and cannot have a single operator '&' applied to it (because there is no memory address).

 { register int hocphi ; } 

The register class should only be used for variables that require quick access such as counters. Note also that a variable defined with 'register' does not mean that the variable is stored in the register. That is, it can be stored in the register depending on the hardware and the execution limit.

Static storage class in C / C ++

The static storage class in C / C ++ tells the compiler to keep a local variable that exists for the entire lifetime of the program instead of creating and destroying the variable each time it enters and exits the variable scope. Therefore, variables have static that allows it to maintain values ​​between function calls.

Static storage classes can also be applied to global variables (global). When applied to global variables, it tells the compiler that the scope of the global variable is limited to the file in which it is declared.

In C / C ++, when static is used on the data member of the class, it causes: only one copy of that member is shared by all objects in its class.

 #include // phan khai bao ham void func ( void ); static int biendem = 10 ; /* Day la bien toan cuc */ main () { while ( biendem --) { func (); } return 0 ; } // Phan dinh nghia ham void func ( void ) { static int i = 5 ; // Day la bien cuc bo dang static i ++; std :: cout << "i co gia tri la " << i ; std :: cout << " va biendem co gia tri la " << biendem << std :: endl ; } 

Running the above C / C ++ program will produce the following results:

Storage class in C / C ++ Picture 1

The class stores extern in C / C ++

The extern class stored in C / C ++ is used to provide a reference of a global variable seen by ALL program files. When you use 'extern', the variable cannot be initialized, when it points to the variable name at a storage class location that was previously defined.

When you have multiple files and you define a global variable or function in a file and also want to use it in other files, extern is used in another file to provide the reference of the defined variable or function. Remember, extern is used to declare a global variable or function in another file.

The class that stores extern is commonly used when there are two or more files that share the same variable or global function. See example with the following two files:

First file: extern1.cpp

 #include int biendem ; extern void vidu_extern (); main () { biendem = 5 ; vidu_extern (); } 

The second file: extern2.cpp

 #include extern int biendem ; void vidu_extern ( void ) { std :: cout << "Gia tri biendem la " << count << std :: endl ; } 

Here, the keyword extern is being used to declare biendem in another file. Now compile these two files as follows:

 $g ++ extern1 . cpp extern2 . cpp - o write 

It will create the executable write program, try to write and test the following result:

 $ ./ write 5 

Mutable storage class in C / C ++

Mutable storage classes in C / C ++ only apply to class objects, which will be discussed in later chapters. It allows a member of an object to override. That is, a member that is mutable can be modified by a const member function.

According to Tutorialspoint

Previous post: Modifier in C / C ++

Next lesson: Operator in C ++

4 ★ | 2 Vote

May be interested

  • Class properties in HTMLClass properties in HTML
    in html, class properties are used to identify one or more class names for html elements.
  • Class (class) and Object in C ++Class (class) and Object in C ++
    the main purpose of c ++ is to add object orientation to the c programming language and classes which are central features of c ++ that support object-oriented programming and are often called types user defined (user-defined).
  • Managing Windows networks using Script - Part 13: The script returns all valuesManaging Windows networks using Script - Part 13: The script returns all values
    in the previous article of this series we came up with a script called displayclassproperties.vbs, which shows the names of the properties of the wmi class. this is what the script content is, by using win32_bootconfiguration as a class, we are connecting to the wmi nickname.
  • Static member of class in C ++Static member of class in C ++
    we can define class members as static using the static keyword in c ++. when we declare a member of a class that is static, that is, no matter how many objects of the class are created, there will only be one copy of the static member.
  • Learn about Class, Object and Instance in object-oriented programmingLearn about Class, Object and Instance in object-oriented programming
    class, object and instance are the basic concepts in object-oriented programming (oop) that learners need to understand.
  • Calculate inheritance in C #Calculate inheritance in C #
    one of the most important concepts in object-oriented programming is inheritance. inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. this also provides an opportunity to reuse code features and faster execution time.
  • Access Modifier for class in C ++Access Modifier for class in C ++
    data hiding is one of the important features of object-oriented programming that allows to prevent the function of a program from directly accessing the internal representation of a class type.
  • Data storage: That day and nowData storage: That day and now
    the smaller the storage drive size, the more storage capacity ... that is the biggest change in the last 6 decades.
  • How to Do a Presentation in ClassHow to Do a Presentation in Class
    doing a presentation in class can be intimidating, but it does not have to be. this wikihow will give you lots of pointers on how to do a presentation in class with minimal stress. write note cards on index cards. write main ideas on your...
  • Friend function in C ++Friend function in C ++
    the friend function in c ++ of a class is defined outside of that class scope, but it has access to all private and protected members of that class. even if the prototypes for friend functions appear in the class definition, friend functions are not member functions.