Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement left value reference and right value reference in C++

Shulou Source: shulou.com Published: 2022-05-31 22:00:05 10月04日 Update

This article mainly explains "how to achieve left-value reference and right-value reference in C++". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn how to achieve left-value reference and right-value reference in C++.

1. The concept of left value and right value

The left value is a value that can be assigned to the left of the assignment sign; the left value must have an entity in memory

The right value should be taken out to the right of the assignment number and assigned to other variables; the right value can be in memory or in the CPU register.

When an object is used as a right value, its content (value) is used, and when it is used as a left value, its address is used.

2. Citation

Reference is an optimization of C++ syntax, and the essence of reference is realized by pointers. Reference is equivalent to an alias for a variable.

References can change the direction of the pointer and the value that the pointer points to.

Basic rules for referencing:

The reference must be initialized when it is declared, and once bound, the reference cannot be bound to another object; that is, the reference must be initialized and cannot be redefined

All operations on references are equivalent to operations on the original object.

3. Left value reference and right value reference

3.1 left value reference

Basic syntax for left-value references: type & reference name = left-value expression

3.2 right value reference

Basic syntax for right-value references type & & reference name = right-value expression

Right-value references are often used by enterprise developers in code optimization.

There can be no spaces in the "&" referenced by the right value.

# include using namespace std; int main () {cout

Tags: Cantilever syntax content variables right address register object pointer learning code memory pointing equal sign expression or operation bad because Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Huawei vpn MySQL macOS