Const, static, online

Source: Internet
Author: User

Const:

1. Define variables. You can write the following statements:

Type const valuename = value;
Const type valuename = value;

2. When used as a global variable and in other files: (by default, const is local)

1. cpp: extern const type valuename = value; Definition

2. cpp: extern const type valuename; External Declaration

The above two const and type can be reversed.

3. the pointer uses Const.

If the const is on the left side of *, const is used to modify the variable pointed to by the pointer, that is, the pointer points to a constant;
If const is on the right side of *, const is to modify the pointer itself, that is, the pointer itself is a constant.

4. Use const in return values of function parameters

Category 5 and const:
Class
{
...
Const int nvalue; // The member constant cannot be modified.
...
A (int x): nvalue (x) {}; // values can only be assigned to the initialization list.

Void function () const; // constant member function, which does not change the member variable of the object.

// You cannot call any non-const member functions in the class.


}

1) modify a member variable: It indicates a member constant and cannot be modified. It can only be assigned a value in the initialization list.

2) the const member function is not allowed to modify any data member of its object. Non-const member variables can be used in the function, but cannot be modified. You cannot call any non-const member functions in the class. (Because any non-const member function will attempt to modify the member variables .) Const member function declaration and definition cannot be less

3) const Class Object, pointer, and reference: only the const member functions of the class can be called. member variables can be used, but the value cannot be changed. Therefore, the most important function of the const modifier is to restrict the use of the const object.

 

Class A {public: const int nvalue; // The member constant cannot be changed to int M_a; A (int x, int A): nvalue (x) {M_a = ;}; // only void func () const can be assigned to the initialization list; // constant member function, which does not change the member variable of the object. // you cannot call any non-const member functions in the class. }; Void a: func () const {cout <M_a <Endl; cout <nvalue <Endl;} int main () {const a AA (1, 2 ); cout <AA. m_a <Endl; AA. func (); Return 0 ;}

Static:

 

 

 

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.