Tag Archives: mutable

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;

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

C++ interview questions on storage classes like static, register, auto, extern, volatile, mutable

C++ interview questions on storage classes like static, register, auto, extern, volatile, mutable

Storage classes
What are C++ storage classes?
auto
register
static
extern
auto: the default. Variables are automatically created and initialized when they are defined and are destroyed at the end of the block containing their definition. They are not visible outside that block
register: a type of auto variable. a suggestion to the compiler to use a CPU register for performance
static: a variable that is known only in the function that contains its definition but is never destroyed and retains its value between calls to that function. It exists from the time the program begins execution
extern: a static variable whose definition and placement is determined when all object and library modules are combined (linked) to form the executable code file. It can be visible outside the file where it is defined.
What are storage qualifiers in C++ ?
They are..
const
volatile
mutable
Const keyword indicates that memory once initialized, should not be altered by a program.
volatile keyword indicates that the value in the memory location can be altered even though nothing in the program
code modifies the contents. for example if you have a pointer to hardware location that contains the time, where hardware changes the value of this pointer variable and not the program. The intent of this keyword to improve the optimization ability of the compiler.
mutable keyword indicates that particular member of a structure or class can be altered even if a particular 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

Reference:

What’s the difference between pointer and reference
Reference is to one object can not be changed to another object
Reference should be initialized always.
Any other Differences??

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!