The role of static, and const, and reference types in C + +

Source: Internet
Author: User

This is my own summary at this stage, I hope you will add:

First say static:

It represents a static

So 1, hidden, global variables or global functions plus static are inaccessible to other files, only in the current file

2, keep the contents of the variable persisted, because the static variable is stored in the global zone once initialized to know that the program ends the menu user is released

3, the default initialization variable value is 0

4. When defining a member variable or member function with static, it becomes a member of the class, all objects are shared

The role of the const keyword

1. The modifier variable const int A=10,A is a constant that is initialized once at the time of declaration and cannot be assigned in the future.

2, modifier pointers are divided into constant pointers, and pointer constants

A constant pointer is a pointer to a constant, and the pointer must be a constant const int * p

Pointer constant, the content of the pointer is constant, that is, the pointer cannot change int *const P

3, modify the reference. Make a reference a regular drink, you cannot modify the value of a variable by reference

4, modify the array. Array becomes Changshu group, cannot modify the value of the element inside, can only access

5, modifier function parameters. Increase efficiency by not generating copies of variables

modifier function parameters. Make a member function of a Class A const member function, you cannot modify the value of a class's member variable in a function

const int & Print (const int a) const

The first const modifier returns a value that returns a constant reference and cannot modify the value of the return value

The second const modifier parameter, which means that the value of the parameter can no longer be changed in this function

The third const modifier, which indicates that the function is a constant function, cannot modify the value of a member variable in a class

At the same time, this function can only call the constant member variable

Reference type

Reference type, which is an alias for the variable, format: type identifier & function name (formal parameter list and description) {function Body}

1. You cannot return a reference to a local variable because the local variable is destroyed after the function returns, so the returned reference becomes a wild pointer and the program crashes

2. You cannot return a reference to the memory that is allocated by new, which may cause a leak to be stored

3, can return the reference of the class member, the single best is the const

This article is from the "Simon Eat Cattle" blog, please be sure to keep this source http://ximenchiniu.blog.51cto.com/9503623/1683404

The role of static, and const, and reference types in C + +

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.