- Expert C++
- Vardan Grigoryan Shunguang Wu
- 156字
- 2021-06-24 16:34:02
Lvalue references
Before understanding why rvalue references were introduced in the first place, let's clear things up regarding lvalues, references, and lvalue-references. When a variable is an lvalue, it can be addressed, it can be pointed to, and it has a scoped storage duration:
double pi{3.14}; // lvalue
int x{42}; // lvalue
int y{x}; // lvalue
int& ref{x}; // lvalue-reference
ref is an lvalue reference, a synonym for a variable that can be treated as a const pointer:
int * const ref = &x;
Besides the ability to modify the objects by a reference, we pass heavy objects to functions by reference in order to optimize and avoid redundant object copies. For example, the operator+ for the Warehouse takes two objects by reference, thus making it copy addresses of objects rather than full objects.
Lvalue references optimize the code in terms of function calls, but, to optimize temporaries, we should move on to rvalue references.
- SQL學習指南(第3版)
- HBase從入門到實戰
- Python進階編程:編寫更高效、優雅的Python代碼
- 精通API架構:設計、運維與演進
- Learning Neo4j 3.x(Second Edition)
- 人人都是網站分析師:從分析師的視角理解網站和解讀數據
- Gradle for Android
- Python編程從0到1(視頻教學版)
- Building Serverless Web Applications
- Laravel Application Development Blueprints
- R語言:邁向大數據之路(加強版)
- 30天學通C#項目案例開發
- 計算機應用基礎
- Python Business Intelligence Cookbook
- 前端程序員面試筆試真題與解析