const, static in C/

Source: Internet
Author: User

First, the const

In C + +:

1, Constant object: the creation of the object when the Const keyword, the object can not be modified, there is a constant attribute, it means that the entire object all variables cannot be changed

2, the normal object cannot call the ordinary function, the call member function is equivalent to the object's this pointer to him, there is the risk of being modified

3, the constant object can only call the constant function (the function body before the keyword called constant function), ordinary objects can also call the constant function, the constant function is equivalent to the this pointer added a const property

4, the constant function and the ordinary function can be overloaded, will not conflict

5, if a member does need to modify, and he was modified by the const, you can add a keyword to the member mutable, so that even if the constant object calls the constant function can still modify the member

C:

Const: Constant, add a protection for the variable save, the protection variable can not be displayed modification, improve the security of the program, when the program does not need to modify the parameter value, should add the const attribute;
Const can define constants, and const modifies the initialized global variables as constants;

Second, static

C + +: Static members and static member functions

1, the class member is static modification, it will be stored in the BSS segment (this segment is stored by the compiler and the size is fixed), in the program dynamically created object when its static members can not be created, all class objects share a static member

2, static members can only be declared in the class cannot be defined in the class (must be defined outside the Class) type class name:: Static member name

3. A static member is a global variable declared in a class and can be accessed in any location using the class name:: Static member name

4. Static member functions: member functions in a class become static member functions after being modified by static, all members share a copy of static member functions

5. Static member functions do not pass the this pointer, and can also call static member function class names without objects:: Static member functions (parameters)

C:

Static
When you modify a global variable, the scope changes from the entire program to the. c file in which he resides;
1, Static local, block:
The storage location is changed from the stack to the static segment, and the life cycle changes from the definition to the end of the program, but the scope is the same; the default value is 0; it can only be initialized once;

Static functions:
static int func (int num);
Static changes the external function into a memory function, only within the. c file where he resides;

Static modifies the benefits of global variables and functions to prevent conflicts with other functions, variable names

const, static in C/

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.