foreword
- Hello everyone, welcome to the YY drop C++ series, a warm welcome!
- As the title suggests, the main content of this chapter is mainly to "quote " this assassin! The following is the outline~
1. Citation
1. Meaning and characteristics
References, that is, aliases . Its biggest feature is that the compiler will not open up space for reference variables , they share the same space.
2. The difference between references and pointers (mainly)
1. The reference must be initialized when used .
2. After the reference refers to an entity during initialization , it cannot refer to other entities again , and can only be assigned.
3. References are safer to use .
Graphic:
3. Practical use of references
1. References as parameters
When used as an output parameter, when facing large objects/deep copy objects , since there is no need to open up additional space for copying , the efficiency can be improved
2. Reference as return value
Small directory:
- Applicable scene
- Modify the return value + get the return value (to make the address book code more concise)
1. Inapplicable scenarios:
Applicable scenarios: (Stack frames in the static area are not destroyed)
2. Practical application
- In the address book, using the traditional method, you need to "find" the corresponding pos position and then "modify"
- And using "reference as return value", you can directly modify the found value.
Original operation:
Operation after improvement:
3. Permission issues during citation (panning, zooming out, zooming in)
- First of all, we need to know that temporary variables are constant , const modified types are also constant , and static data stored in the static area is also constant .
- The permissions of const and static are theoretically equal , while the permissions of temporary variables are lower than the two.
- There can only be a level of authority and reduced authority, and there cannot be a situation of increased authority. In layman's terms: Those with low authority cannot alias those with strong (regular) authority.
Permission-related knowledge points: [ Higher permissions are const and quantities with constant attributes, and lower permissions are ordinary data ]
- Those with high or equal authority can alias/address another quantity ( restriction and translation of authority )
- Those with low authority cannot alias/address another quantity ( enlargement of authority )
Graphic:
1. Situation of flat level and reduced authority
2. Permission translation
3. Authority amplification
PS: const cannot be modified in principle, but it can be modified directly by finding its space . (pointer/alias)