C ++ all keywords

Source: Internet
Author: User
Tags float double switch case
C ++ all keywords

(1) Auto

This keyword is used to declare that the lifetime of a variable is automatic, and variables not defined in any class, structure, enumeration, union, or function are considered global variables, the variables defined in the function are considered as local variables. This keyword is rarely written, because all variables are auto by default.

(2) Register

This keyword command is used by the compiler to store variables in the internal registers of the CPU as much as possible, instead of accessing the variables through memory addressing to improve efficiency.

(3) static

Two common uses:
1> count the number of times a function is called;
2> reduce the overhead of local array creation and assignment. Variable creation and assignment require a certain amount of processor overhead, especially Storage types containing many elements, such as arrays. In some functions that contain many variables and are frequently called, you can declare some arrays as static types to reduce the overhead of creating or initializing these variables.

Detailed description:
1> variables will be placed in the global storage area of the program, so that the original values can be maintained during the next call. This is the difference between stack variables and heap variables.
2> the variable uses static to notify the compiler that it is only visible within the scope of the variable. This is the difference between it and global variables.
3> when static is used to modify a global variable, it changes the scope of the global variable so that it cannot be extern by other programs and is restricted to the current file, however, the storage location remains unchanged in the Global static storage zone.

Note:
1> If the global variable is only accessed in a single c file, you can change the variable to a static global variable to reduce the coupling between modules;
2> If the global variable is only accessed by a single function, you can change the variable to the static local variable of the function to reduce the coupling between modules;
3> when designing and using functions that access dynamic global variables, static global variables, and static local variables, you need to consider the re-import problem (the same output should be generated if the input data is the same)

(4) const is forced to protect things modified by const, which can prevent unexpected changes and improve program robustness. It can modify the parameters, return values, and even the definition body of a function.

Purpose:
1> modify input parameters
A. For non-Internal data type input parameters, the "value transfer" method should be changed to "const reference transfer" to improve efficiency. For example, change void func (A) to void func (const A & ).
B. For internal data type input parameters, do not change the "value transfer" method to "const reference transfer ". Otherwise, the function can not improve the efficiency, but also reduce the comprehensibility of the function. For example, void func (int x) should not be changed to void func (const Int & X ).
2> Use const to modify the return value of a function
A. if the const modifier is added to the return value of a function in the "pointer passing" mode, the content of the function return value (that is, the pointer) cannot be modified. The return value can only be assigned to the same type pointer with the const modifier.
For example, const char * getstring (void );
The following statement causes a compilation error:
Char * STR = getstring (); // cannot convert from 'const char * 'to 'Char *';
The correct usage is:
Const char * STR = getstring ();
B. If the return value of a function adopts the "value transfer mode", since the function copies the return value to an external temporary storage unit, adding the const modifier has no value. For example, do not write the int getint (void) function as const int getint (void ).
3> In the const member function declaration, the const keyword can only be placed at the end of the function declaration, indicating that the class member does not modify the object.

Note:
Const type M; // modify m to unchangeable
Example:
Typedef char * pstr; // New Type pstr;
Char string [4] = "ABC ";
Const char * P1 = string;
P1 ++; // correct. The above modifier is * P1, and P1 is variable.
Const pstr P2 = string;
P2 ++; // error. The above modifier is P2, P2 is unchangeable, and * P2 is variable.
Similarly, when the const modifies the pointer, this principle will not be confused.

Const int * value; // * The value is unchangeable and the value is variable.
Int * const value; // The value is unchangeable. * The value is variable.
Const (int *) value; // (int *) is a type, value is unchangeable, * value is variable
// Logically, the compilation fails. tydef int * newtype is required;
Const int * const value; // * value and value cannot be changed.

(5) Volatile

It indicates that the value of a variable may be changed externally. When using this variable, the optimizer must carefully read the value of this variable every time instead of using the backup stored in the register. It can be applied to basic types such as int, Char, long... and C structures and C ++ classes. When the structure or Class Object is modified using volatile, all the members of the structure or class are considered volatile.
This keyword is often used in a multi-threaded environment, because the same variable may be modified by multiple threads when a multi-threaded program is compiled, and the program synchronizes various threads through this variable.
Simple Example:
DWORD _ stdcall threadfunc (lpvoid signal)
{
Int * intsignal = reinterpret_cast (signal );
* Intsignal = 2;
While (* intsignal! = 1)
Sleep (1000 );
Return 0;
}
Set intsignal to 2 when the thread starts, and then wait until it exits when intsignal is 1. Obviously, the intsignal value must be changed externally; otherwise, the thread will not exit. However, the thread does not exit during actual operation. Even if the value of the thread is changed to 1 in the external department, you can see the corresponding pseudo assembly code:
MoV ax, signal
Label:
If (ax! = 1)
Goto label
For the C compiler, it does not know that this value will be modified by other threads. Naturally, it is cached in the register. The C compiler does not have the thread concept. Volatile is used in this case. Volatile indicates that the value may be changed outside the current thread. That is to say, we need to add the volatile keyword before intsignal in threadfunc. At this time, the compiler knows that the value of this variable will change externally, so it will read the variable again every time it is accessed, the following pseudo code shows how the loop is made:
Label:
MoV ax, signal
If (ax! = 1)
Goto label

Note: A parameter can be either const or volatile, because it may be unexpectedly changed. It is const because the program should not try to modify it.

(6) extern indicates "external" · It is used to tell the compiler that this variable may not exist in the current file, but it must exist in a source file in the project or the output of a DLL. C ++ keywords (different from C keywords)

1. types included in C ++:

Bool True False

2. keywords used in type declaration

Class

Public

Private

Protected

Friend

Mutable

Explicit

This

3. Virtual and polymorphism and rtti

Virtual

Typeid ()

Static_cast <> ()

Dynamic_cast <> ()

Reinterpret_cast <> ()

4. Generic programming

Template <>

Typename

5. Global functions and member functions

Inline

6. Heap Management

New

Delete

7. Exception Handling

Try {}

Catch (){}

Throw ()

[Edit this section] Computer Language

  KeywordsIs used to identify the specific data item destination value of each record in the file.

  KeywordsIt is a special identifier defined in the computer language in advance. It is sometimes called a reserved word. The system defines the following keywords:

Const (constant) dim (defined) as (for) mod (Modulo)

And (and) or (OR) Not (not)

If (IF) Then (then) else (otherwise)

Stop end)

Select case (condition) is (yes)

For (count) to (to) Step (step size)

Byref (Transfer address) byval (transfer value)

Sub (subroutine) function (function) Exit (Exit)

Do (DO) loop (loop) until (unless) while (when) Wend (when the end)

Let (LET) Call (call)

REM (comment)

INTEGER (integer) Long (long integer) single (single precision decimal) double (double precision, decimal) Boolean (Boolean) string (string, text)

Me (me) Private (private) Public (public)

Note: names and keywords in VB are case-insensitive. In C, all are lowercase letters.

Keyword survey statistics for C, C ++, and Java:

C

, C ++, Java

Common keywords ----- 22

Items

Int char float double short long

If else

Switch case default

For while do

Void return

Continue break

Const GOTO (unused in Java, but reserved words)

Volatile static

C

And C ++

Keywords (except the three) ---- 10

Items

Unsigned signed

Struct Enum Union

Auto register extern

Sizeof typedef

C ++

And Java

Common keywords ---- 11

(9

)

Class new public private protected this try throw catch (True False)

C ++

Unique keywords (except Java

In total) ---- 20

Items

ASM bool explicit export friend inline mutable operator Template

Typeid virtual warch_t Delete namespace typename using

Const_cast dynamic_cast reinterpret_cast static_cast

Java

Unique keywords (except C ++

In total) ---- 17

Items

Abstract Boolean byte extends final finally implements import instanceof Interface

Native package super synchronized throws transient strictfp

NULL (used only in Java, similar to true or false, not a keyword)

So:

C has 22 + 10 = 32 keywords

C ++ has 22 + 10 + 11 + 20 = 63 keywords

Java has 22 + 9 + 17 = 48 keywords

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.