C + + FAQ (iii)--related examples of macro definition, const, static, sizeof __c++

Source: Internet
Author: User
Tags modifier
first part macro definition

Syntax format for macro definition: #define < identifier >< string, where the identifier is called a symbolic constant, also known as the "macro name." Preprocessing work is also called macro expansion. Macro definitions do not consume memory and compile time.
  
Example 1: Use #define to implement macros and find the maximum value.

#define MAX ((x) > (y)) (X,y) (x):(y))

Note in the macro you need to carefully enclose the parameters in parentheses, because the macros are simply text replacements, and then give an example to illustrate what you just said.

Example 2:

#define SQR (x) (X*X)
int main ()
{
    int a,b=3;
    A = SQR (b+2);
}

Parsing: The SQR (x) function defined here is to obtain the two-point x, and to get the result of (3+2) * (3+2) =25 in line 5th. However, the expansion of the macro definition is in the preprocessing period, at which point B is not assigned, so the expansion result is a= (b+2*b+2) = 11. In order to achieve the original purpose, you need to change the original macro definition of the second half ((x) * (x)).

Example 3: Use a macro to define the number of elements that an array contains.

#define ARR_SIZE (a) ((sizeof (a))/(sizeof (A[0)))
the use of the second part of the const

the role of the const:
1.const for defining constants
2.const modifier function Form parameter: When the input parameter is a user-defined type and an abstract data type, changing the value to "Const & Pass" can improve efficiency.
For example, void Fun (A const &a); Passing a reference does not need to produce a temporary object, the construction, replication, and destructor of the temporary object is saved, so add the const to prevent the reference from changing the value of a.
The return value of the 3.const modifier function
4.const Modifier class member function: Any function that does not need to modify the data member should use the const modifier, int getcount (void) const;

Example 4: use of const

int main ()
{
    const int x=1;
    int b=10;
    int c=20;

    Const int* a1=&b;
    int* Const a2=&b;
    Const int* Const a3=&b;

    x=2;   
    X is a shape variable and cannot change its value, and if X is not initialized, then x is a random number
    a1=&c;
    The const is on the left side of the int* to modify the variable that the pointer points to, that is, the pointer to a constant.
    //above is the modified pointer A1 itself, not the content pointed to by the pointer.
    *a1=1;
    This command is not allowed to modify the content that the pointer points to.
    a2=&c;
    Const on the right, modifies the pointer, that is, the pointer itself is a constant, the wrong sentence.
    *a2=1;
    The pointer points to 1, allowing.
    a3=&c;
    *a3=1;
    Both of these are const constants and are not allowed.
}
Part Three static variables

Example 5: the role of static

1. In the function body, a variable declared as static maintains its value unchanged during the invocation of the function.
2. Within the module, a variable declared as static can be accessed by all functions within the module, but not by other functions outside the module.
3. Within a module, a function that is declared static can only be invoked by other functions within the module. That is, the function is limited to the module within which it is declared.

Example 6:

Staic the difference between global variables and ordinary global variables: Static global variables are initialized only once, preventing them from being referenced in other file cells;
The difference between a staic local variable and a normal local variable: the static local variable is initialized only once, and the next one is based on the last result value.
The difference between a staic function and a normal function: The static function has only one copy in memory, and the normal function maintains a copy of each call. Part IV: sizeof

Example 7: using sizeof to compute the space occupied by ordinary variables

Char str[]= "Hello";
char* p = str;
int n = ten;
void* q = malloc (MB);

sizeof (str) = ___;  STR represents an array, and the result is the total space occupied by the array, noting that the array has a last element to hold the string terminator, 6
sizeof (p) = ___;  Pointer variable, 4
sizeof (n) = ___;  INT-shape variable, 4
sizeof (q) = ___;  Point to 100 bytes of heap memory, 4
//If the array variable is sizeof in the passed function, there is no difference from the operation of the pointer; otherwise, the total size of the entire array is consumed.
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.