Learning notes for the basics of C + + grammar

Source: Internet
Author: User

What is the meaning of the keyword const? What about the modifier class? Static functions for classes? and the role of extern c.

Const means "read Only", what do the following statements mean?  

The code is as follows Copy Code
const int A;  
int const A;  
const int *a;  
int * const A;  
int const * a const;

The first two function is the same, a is a constant integer number. The third means that a is a pointer to a constant integer (that is, the integer is not modifiable, but the pointer can). The fourth meaning a is a constant pointer to an integer (that is, the integer that the pointer points to can be modified, but the pointer is not modifiable). The last one means that a is a constant pointer to the constant integer number (that is, the integer to which the pointer points is not modifiable, and the pointer is not modifiable).

Conclusion:

?; The role of the keyword const is to convey very useful information to the person who reads your code, in fact, declaring a parameter as constant is to tell the user the purpose of the parameter. If
You have spent a lot of time cleaning up the rubbish left by others, and you will soon learn to thank this extra information. (Of course, people who know how to use the const programmer rarely leave the rubbish to be cleared.)
The reason. )
?; By giving the optimizer some additional information, using the keyword const may lead to more compact code.  
?; A reasonable use of the keyword const allows the compiler to naturally protect those parameters that you do not want to change, and prevent them from being modified by unintentional code. In short, this can reduce the appearance of bugs.

(1) To prevent a variable from being changed, you can use the Const keyword. When you define the const variable, it is usually necessary to start the
Start, because there is no chance to change it again;
(2) For pointers, you can specify that the pointer itself is a const, or you can specify that the data indicated by the pointer is a const, or both
Set as const;
(3) In a function declaration, const can modify the formal parameter to indicate that it is an input parameter and cannot change its value within the function ;
(4) For a member function of a class, if it is specified as a const type, it indicates that it is a constant function and cannot modify the member variables of the class;
(5) For a member function of a class, it is sometimes necessary to specify that its return value is a const type so that its return value is not a "left" value.

What is the meaning of the keyword volatile? And give three different examples.

A variable defined as volatile means that the variable may be unexpectedly altered so that the compiler does not assume the value of the variable. Precisely, the optimizer is using the
This variable must be carefully re-read the value of the variable each time, instead of using a backup stored in the register. Here are a few examples of volatile variables:

?; Hardware registers for parallel devices (e.g., status registers)
?; A non-automatic variable that is accessed in an interrupt service subroutine (non-automatic variables)
?; A variable shared by several tasks in a multithreaded application

?; Can a parameter be either a const or a volatile? explain why.  
?; Can a pointer be a volatile? explain why.  

Here is the answer :
?; Yes. An example is a read-only status register. It is volatile because it can be changed unexpectedly. It is const because the program should not attempt to modify it.  
?; Yes. Although this is not very common. An example is when a medium service subroutine fixes the pointer to a buffer.

The role of the Static keyword:

(1) The function body of the static variable is the function body, unlike the auto variable, the memory of the variable is only allocated once,
So its value remains the last value at the next call;
(2) The static global variable in the module can be accessed by functions in the module, but not by other functions outside the module.
(3) The static function within the module can only be invoked by other functions within the module, and the scope of use of this function is limited to the declaration
Within the module of it ;
(4) A static member variable in a class is owned by the entire class and has only one copy of all objects of the class;
(5) A static member function in a class is owned by the entire class, and this function does not receive the this pointer, and thus can only access the static member variable of the class.

The role of extern "C"

(1) The function or variable defined by extern "C" is an extern type;

extern is a keyword that indicates the scope (visibility) of functions and global variables in the C + + language, and the keyword tells the compiler
The functions and variables that they declare can be used in this module or in other modules.

(2) The variables and functions modified by extern "C" are compiled and connected according to the C language;

e the idiomatic method of Xtern "C"

(1) in C + + referencing the functions and variables in the language, in the C language header file (if cExample.h), you need to enter
The following processing is done:

The code is as follows Copy Code
extern "C"
{  
#include "cExample.h"
}  

In the C language header file, the external function can only be specified as an extern type, and the extern "C" declaration is not supported in C language.
A compile syntax error occurs when an extern "C" is included in the. c file.

(2) When referencing functions and variables in C + + language, the header file of C + + must be added extern "C", but in C language does not
To directly reference the header file that declares extern "C", you should only declare the extern "C" function defined in C + + as
the extern type.

10. Why does the standard header file have a structure similar to the following?  

The code is as follows Copy Code
   ifndef __incvxworksh  
   #define __incvxworksh   
   #ifdef __cplusplus  
   extern" C "{  
   #endif   
  /*...*/   
   #ifdef __cplusplus  
  }  
   #endif   
   #endif/* Incvxworksh */ 

Obviously, the compilation macro in the header file #ifndef __incvxworksh, #define __INCVXWORKSH, #endif
is to prevent the header file from being repeatedly referenced.

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.