top of page

Get auto trading tips and tricks from our experts. Join our newsletter now

Thanks for submitting!

Writer's pictureBryan Downing

C++ Storage classes and qualifiers like auto, extern, register, volatile, const, mutable

C++ Storage classes and qualifiers like auto, extern, register, volatile, const, mutable

C++ storage classes include: Auto: This is the default where variables are automatically created when they are defined and are destroyed at the end of a block containing their definition. They are also not visible outside of their defined block. Register: This is similar to auto variable but suggests to the compiler to use a CPU register for performance. Extern: A static variable that is whose definition and placement is determined when object and library modules are combined (also known as linking) to form the executable code file. It can also be visible outside of where it is defined as well. Both C and C++ require that symbols are only defined in a single translation unit (file.c|file.cpp). The extern keyword tells the compiler that a symbol is only a declaration – it is defined somewhere else. If you only ever include a header in a single .c-file, you can get away without the extern. However, if you include it in more than one, the linker will see the same symbol defined in more than one translation unit (one for each .c[pp] file including the header) and rightly flag it as an error. Storage qualifiers: Const keyword indicates that memory once initialized cannot be altered by the program. Volatile keyword indicates that the value at a particular memory location can be altered event though in the program modifies its content. Volatile is to improve the compiler optimization. The volatile keyword is a type qualifier used to declare that an object can be modified in the program by something (external resources) such as the operating system, the hardware, or a concurrently executing thread. For example, Mutable keyword indicate particular data member of a structure or class can be altered even if a particular variable is defined in a class member function as being const. structure variable, class, or class member function is constant. struct data { char name[80]; mutable double salary; } const data MyStruct = { “Satish Shetty”, 1000 }; //initlized by complier strcpy ( MyStruct.name, “Shilpa Shetty”); // compiler error MyStruct.salaray = 2000 ; // complier is happy allowed

? int volatile nVint;

4 views0 comments

Comments


bottom of page