Phần 1
Lvalue
An lvalue identifies an existing object.
int price = 100;
price = 101;price is an lvalue because it identifies an existing object.
Phần 2
Lvalue Reference
An lvalue reference is another name for an existing object.
int& ref = price;
ref = 102;Changing ref changes price because both names refer to the same object.
Phần 3
Function Parameter
void update(double& value);A non-const lvalue-reference parameter allows a function to modify the original object supplied by the caller.
Phần 4
Const Reference
void inspect(const double& value);Use a const lvalue reference to read an object without copying it and without allowing the function to modify it through that reference.
Phần 5
Key Rules
Passing a parameter by value creates a separate parameter object.
A non-const
T&refers to the original object and can modify it.A
const T&reads the original object without copying or modifying it through that reference.
Mã mẫu hoàn chỉnh
cpp11/19_21_lvalue/main.cpp
#include <iostream>
void update(double& price)
{
price += 0.5;
}
int main()
{
double bid = 100.0;
double& ref = bid;
update(ref);
std::cout << bid << '\n';
}Tự kiểm tra · không chấm điểm
Bạn đã hiểu những điểm nào?
Bài này chưa có danh sách tự kiểm tra trong nguồn.