In C++, there is a selection of expert keywords that is not well known but used frequently in specialized situations. These keywords are important, since many interviewers like to ask related questions to gauge your level of knowledge.
The keyword volatile is often used as a modifier to a variable. This keyword serves two purposes, one is related to static compile time optimization while the other relates to run-time program execution changes.
During compile-time, the compiler may perform optimization to improve the quality of the program's execution code. For example, it may simplify complex expressions, cascade function calling, or shift variable declaration/initialization. While the optimization can be extremely helpful, in some situations it may lead to incorrect behaviour, especially in a multi-threaded program.
Consider the code fragment: an integer variable declaration and initialization of 10. Somewhere down the road, we use that variable in a conditional clause, perhaps comparing it to the same value (10). No where else in the program is the variable used or modified. The compiler may remove the variable delcaration and the conditional clause that uses it as well, since it always evaluates to true (recall the variable is used once and not modified elsewhere). What if, in a multi-threaded program, an obscure method is used to change the value sometime between the time of its initialization and use, which causes the conditional to evaluate to false? We have a different program flow!
In the given situation, what the compiler sees at compile time is different from what happened during runtime. These situations are not uncommon, for example, some programs use flags or counters within a loop structure that may be changed by another thread to signal an exit condition. For instance, child threads of a webserver that process requests.
So, what we have discussed is the application of the keyword in both situations, compile-time and run-time scenarios. In effect, it tells the compiler (during compile-time), not to optimize the variable, since it is volatile (may change unexpectedly during run-time).
This is an interesting keyword, which is not oftenly used. It is used only during compile-time and has no effect during run-time. The keyword allows changes even within a const function.
Recall that a const member function forbids changes to the object's data values (member variables). The violation checks are performed only during compile-time, since all const information is lost during run-time. Afterall, memory addresses can be overwritten by any source.
However, with this keyword, even within a const member function, the variable is allowed to be reassigned.
There is no particular reason to use such a variable, it is simply included with the programming language for flexibility. Though, it may indicate a poor design, or the only possible (temporary) solution to a particular problem.
To understand static, you must be familiar with internal linkage and translation unit. Internal linkage occurs within a translation unit. A translation unit is the current unit that the compiler is working with, normally a source file and any headers involved. This is not related to linking (external linkage).
The keyword static has multiple uses. I must point out that a static variable does not necessarily refer to a global variable, in contrast to static variables in Java.
The first and most common usage is a static variable. A static variable can be further divided into static local variables or static non-local variables. A static local variable is declared within a code fragment (normally within a function) and this variable is only declared and initialized when the program reaches the particular statement. So, in the event that the execution never reaches the statement, the static local variable is never allocated space. This is different from a static non-local variable.
A static non-local variable is declared outside a code fragment. This variable persists from the initial execution of the program all the way to the end. Thus, space is allocated to it once the program begins. However, a static non-local variable is not a global variable, since it is only visible within the same file as it is declared. If you want a global variable, you need to use the keyword extern.
Another usage of the keyword static, is to make a function static. A static function means that it is visible only within the same file that it is declared.
I like to point out that it is common to use the keyword static to define lazy singleton pattern. We can use it by encapsulating a static local variable within a function that returns that value. That way, only when the object is required will it be allocated. The static declaration statement will only execute once so, the singleton pattern is preserved. Furthermore, it is considered lazy instantiation because the object is allocated only when called for.
The keyword const is often used to denote that the variable cannot be changed after its initialization. Also, initialization is required where the variable is declared for const static non-local variables.
This keyword can also be used as an access modifier for member functions, such as const member functions. It can be used as an access modifier for any variable, with its intended meaning.
The keyword extern is very important, since it allows for external linkage, hence the truncated name.
It is used to modify a variable or function. If either one contains the keyword extern, then it tells the compiler that the definition is located outside. That is, during external linkage, the linker will try to resolve the definition for it. Without it, the compiler will complain about missing definition for variable or function.
In the situation of a variable, if the extern is not used in one of the non-defining translation units, then the compiler will define that variable as unique. Henceforth, there will be two variables, not what the programmer intended.
Wanna try online shopping? Here are stylish but gorgeous miumiu in perfect quality.
?????? ?? http://miumiusale5.webnode.jp/
Get ready to find some exciting new fashion (?????? ??) for yourself.
?????? ?? http://www.monclersaleinfo.jp/
find miumiu,chloe,prada is too many type and you may have no idea about such many type. chloe http://www.chloe-mall.net/
obtain ralph lauren,Following two or three recent visits right here, even so, has started to become an increasing number of stylish. ralph lauren shop http://www.ralphlaurenshop-de.com/
KnG6GW Great, thanks for sharing this post.Really looking forward to read more. Fantastic.