C + + keyword static Register const volatile extern

Source: Internet
Author: User

C + + keyword Static,register,const,volatile,extern
  • Static variables are scoped to a file, space is allocated at the beginning of the program, Space is freed at the end, default is initialized to 0, and its value can be changed when used.
      • static variable or static function, only the code within this file can access it, its name (variable name or function name) is not visible in other files.
      • The value of a static variable generated in the function body can be maintained.

    A member variable in a C + + class is declared as static, which means that it is shared by all instances of the class, that is, when an instance of a class modifies that static member variable, its modified value is seen by all other instances of the class , and static member functions of a class can access only static members (variables or functions).

    In addition, a static member variable of a class must be initialized within the scope of the file in which it is declared to be used, and the private type is no exception.

    Object-oriented static keyword (static keyword in class)

      • Static data members precede the declaration of a data member within a class with the keyword static, which is the static data member within the class. Static data members have the following characteristics: 1) and static data members are treated as members of the class. Regardless of how many objects of this class are defined, static data members have only one copy in the program and are shared by all objects of that type. 2) static data members are stored in the global data area and are shared by all objects in this class, so it does not belong to a particular class object and its scope is visible when no class object is produced, i.e. we can manipulate it when there is no instance of the class, and there are two advantages to using static data members compared to global variables: 1) Static data members do not enter the program's global namespace, so there is no possibility of conflicts with other global names in the program; 2) can implement [information hiding]. A static data member can be a private member, while a global variable cannot;
      • Static member functions It serves the entire service of a class instead of a specific object for a class. A static member function is not associated with any object, so it does not have the this pointer, as compared to a normal function. In this sense, it cannot access non-static data members that belong to a class object, nor can it access a non-static member function, which only calls the rest of the static member functions.

    For static member functions, you can summarize the following points: 1) A function definition that appears outside the class cannot specify a keyword static; 2) static members can access each other, including static member functions accessing static data members and accessing static member functions; 3) Static member functions and static data members can be accessed arbitrarily by non-static member functions; 4) static member functions cannot access non-static member functions and non-static data members

  • Register this keyword command compiler as far as possible the variable exists in the CPU internal registers rather than through memory addressing access to improve efficiency.
  • Const-Modified things are protected by coercion, which can prevent accidental changes and improve the robustness of the program. It can modify the parameters of the function, the return value, or even the definition body of the function.

    Role:

    • modifying input parameters
      • For input parameters of non-intrinsic data types, the "value passing" method should be changed to "const reference passing" in order to improve efficiency, such as changing void Func (a A) to void func (const a&a).
      • For input parameters of an internal data type, do not change the "value passing" method to "Const reference pass". Otherwise, the purpose of improving efficiency is not achieved, and the comprehensible function is reduced, such as void func (int x) should not be changed to void func (const int &x).
    • The return value of the function with the const modifier
      • The contents of a function return value (that is, the pointer) cannot be modified if the return value of the function returned by the "pointer Pass" method is added to the const modifier, and the return value can only be assigned to a const-decorated pointer of the same type.
        for const char * getstring (void), the following statement will have a compilation error: char * str=getstring (); The correct usage is: const char *str=getstring ();
      • If the function return value is in "value passing", the const modifier has no value because the function copies the return value into the external temporary storage unit, such as not to write the function int getint (void) as a const int getint ().
    • In the declaration of a const member function, the const keyword can only be placed at the tail end of a function declaration, indicating that the class member does not modify the object.
           Description: const type m; Modifier M is an immutable example: typedef char * PSTR;    new type of pstr;    Char string[4] = "ABC"; const char *P1 = string; p1++;    Correct, the top modifier is *P1,P1 variable const PSTR p2 = string; p2++;    Error, the top modifier is P2,P2 immutable, *P2 variable same, const modifier pointers with this principle will not be confused. const int *value; *value immutable, value variable int* const value; Value is immutable, *value variable const (int *) value;    (int *) is a type,value immutable, *value variable//logically understood so that compilation cannot be passed and requires TypeDef int* NEWTYPE; The const int* Const Value;//*value,value is immutable to give a const pointer to a non-const object, so it cannot be changed. However, Const cannot be assigned to non-const unless cast first, const int x=100; int *p= (int*) &x;    *p++; Class CX;      Internally there are constructors, declarations such as CX (int r =0) CX Fuction1 () {return CX ();}      Const CX Fuction2 () {return CX ();} Fuction1 () = CX (1); No problem, can be called as Lvalue Fuction2 () = CX (1);    Compile error, const return value is forbidden as an lvalue call. The const pass and return of pointers in functions: int F1 (const char *PSTR); Using the const modifier as a pass-through ensures that the initial value of the passed parameter is not modified by this pointer, const char *F2 ();//meaning that the pointer to the function returns a const object that must be assigned a pointer to a const object that is also const char * Const F3 ();    One more const than above, the meaning of this const is only valid when he is used as an lvalue, it shows that this pointer, in addition to the Const object, itself cannot be modified, so it cannot be treated as an lvalue. For classes first, for const member variables, initialization is only possible in constructors using the Initialize member list, and attempting to initialize a const member variable in the constructor body causes a compilation error.    Initialize member list shape such as: x:: x (int ir): R (IR) {}//suppose R is a const member variable of class X note: Neither the constructor nor the destructor of a class can be a const function. A const member function was created, but still wanted to use this function to change the data inside the object. (a function cannot modify a data member of a Class)//If there is a class called X, which has an INT member variable r, we need to ++r the R with a const member function f (), the code is as follows void X::f () const {(const_cast& Lt X*> (this)), ++r; }//Type cast implementation via the this pointer
  • Volatile indicates that the value of a variable may be changed externally, and the optimizer must carefully reread the value of the variable each time it is used, rather than using the backup stored in the register. It can be applied to basic types such as: Int,char,long ... Also applies to C's structure and C + + classes. When using a volatile modifier on a struct or class object, all members of the struct or class are treated as volatile.volatile, meaning that the value may be changed outside the current thread
  • extern
    • The basic explanation extern can be placed before a variable or function to mark the definition of a variable or function in another file, prompting the compiler to look for its definition in other modules when encountering this variable and function. This behavior tells the compiler that the definition of the variable/function already exists somewhere, so that the compiler goes to the other module to find its definition. In addition, extern can also be used to specify links.
    • The
    • extern "C" uses extern "C" primarily because the C + + language is compiled to implement polymorphism, which combines function names and functions to form a different function name (in short, the compiled function name will be different from the function name you declared earlier). The concept of non-polymorphic in C does not, of course, have this singular name change problem. This is the problem, when you want to call C function in C + +, because the name is different, so it will not find the call of the definition of the function, and thus error. In order to solve this conflict between C and C + +, there is the extern "C". The

      C + + language was originally created as "A better C", but this does not mean that C + + global variables and functions in C + + are compiled and connected in exactly the same way as the C language. As a language compatible with C, C + + retains some of the features of the procedural language (known as "not completely object-oriented"), so it can define global variables and functions that are not part of any class. However, after all, C + + is an object-oriented programming language, in order to support the overloading of functions, C + + to the global function of the processing method and C are obviously different.

      An enterprise has given the following interview question:

       Why do standard header files have a structure similar to the following? #ifndef __incvxworksh #define __incvxworksh #ifdef __cplusplus extern "C" {#endif/*...*/#ifdef __CPLU Splus} #endif #endif/* __incvxworksh */

      Analysis: Obviously, the compiler macro in the header file "#ifndef _incvxworksh, #define _INCVXWORKSH, #endi F "is to prevent the header file from being repeatedly referenced. So what is the role of

       

      ? The

      Answer is to implement mixed programming of C + + and C and other languages. When compiling the CPP program with g++, the compiler defines the macro _cplusplus, which determines whether the extern "C" is required depending on whether the _cplusplus is defined.

date:2014-12-02t11:13+0800

Author:kirchhoff

ORG version 7.9.3f Withemacs version 24

Validate XHTML 1.0

C + + keyword static Register const volatile extern

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.