Latest web development tutorials

C ++ storage class

Storage class defines a C ++ program variables / function range (visibility) and life cycle. These specifiers placed before they modify the type. Listed below are storage class C ++ programs are available:

  • auto
  • register
  • static
  • extern
  • mutable

auto storage class

auto storage class is all local variables default storage class.

   int mount;
   auto int month;

The above example defines two variables with the same storage class, auto can only be used within a function, namely auto only modifies the local variable.

register storage class

register storage class is used to define stored in registers instead of RAM in the local variables.This means that the maximum size of the variable is equal to the size of the register (usually a word), and can not be applied to it unary '&' operator (because it has no memory location).

   register int  miles;

Need only register for quick access to variables, such as counter. It should also be noted that the definition of 'register' does not mean that variable will be stored in a register, it means that the variable may be stored in a register, depending on the limitations of hardware and implementation.

static storage class

static storage class instructs the compiler to keep the existence of local variables in the program's life cycle, without the need to enter it every time you go out of scope and be created and destroyed.Therefore, the use of static local variables can be modified to maintain the value of local variables between function calls.

static modifier can be applied to global variables. When the modified static global variable, the variable will limit the scope of the statement in its files.

In C ++, when used on static class data members, all objects will result in only one copy of the class members are shared.

#include <iostream>
// 函数声明 
void func(void);
static int count = 10; /* 全局变量 */
int main()
    return 0;
// 函数定义
void func( void )
    static int i = 5; // 局部静态变量
    std::cout << "变量 i 为 " << i ;
    std::cout << " , 变量 count 为 " << count << std::endl;

When the above code is compiled and executed, it produces the following results:

变量 i 为 6 , 变量 count 为 9
变量 i 为 7 , 变量 count 为 8
变量 i 为 8 , 变量 count 为 7
变量 i 为 9 , 变量 count 为 6
变量 i 为 10 , 变量 count 为 5
变量 i 为 11 , 变量 count 为 4
变量 i 为 12 , 变量 count 为 3
变量 i 为 13 , 变量 count 为 2
变量 i 为 14 , 变量 count 为 1
变量 i 为 15 , 变量 count 为 0

extern storage class

extern storage class is used to provide a reference to a global variable, the global variable for all the program files are visible.When you use 'extern', who can not initialize a variable, the variable name will point to a previously defined storage location.

When you have multiple documents and define a global variable or function that can be used in other documents, you can useexternin other files to get a variable or function defined reference. It can beunderstood,extern is used to declare a global variable or function in another file.

extern modifier is usually used when there are two or more files share the same global variables or functions of time, as follows:

First file: main.cpp

#include <iostream>
int count ;
extern void write_extern();
int main()
   count = 5;

The second file: support.cpp

#include <iostream>
extern int count;
void write_extern(void)
   std::cout << "Count is " << count << std::endl;

Here, the second fileexternkeyword is used to declare the first count has been defined in the file main.cpp in the. Now, compile both files, as follows:

$ g++ main.cpp support.cpp -o write

This generateswrite executable program attempts to perform write,it will produce the following results:

$ ./write
Count is 5

mutable storage class

mutable specifier applies only to object classes, which will be in the end of this tutorial to explain.It allows members of the object substitute constants. That is, mutable member can be modified by const member functions.