-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19-Reference.cpp
More file actions
40 lines (32 loc) · 815 Bytes
/
19-Reference.cpp
File metadata and controls
40 lines (32 loc) · 815 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//19-Reference
#include <iostream>
//pass original argument rather than making copy.
void f(int &r){
r++;
}
int& g(int &r){
return r;
}
int main(){
//L-Value Reference
int a{1};
//int &b; //error. must initialize.
//int &b{1} //error. lvalue needed.
int &b{a};
int *p = &b;
const int d{1};
std::cout << "a = " << a << std::endl;
std::cout << "b = " << b << std::endl;
std::cout << "*p = " << *p << std::endl;
std::cout << "&a = " << &a << std::endl;
std::cout << "&b = " << &a << std::endl;
std::cout << "p = " << p << std::endl;
f(a);
std::cout << "a = " << a << std::endl;
g(a) = 10;
std::cout << "a = " << a << std::endl;
//R-Value Reference
//int &&x{a}; //error. not rvalue.
int &&x{static_cast<int&&>(a)}; //convert lvalue to rvalue.
std::cout << "x = " << x << std::endl;
}