-
Notifications
You must be signed in to change notification settings - Fork 0
/
constReference.cpp
56 lines (43 loc) · 1.01 KB
/
constReference.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
using namespace std;
int getInt() {
int a = -10;
return a;
}
// Return By Reference is Evil, unless you are a 100% sure that the lifetime of the object won't end after the function call.
// In General avoid it.
// https://stackoverflow.com/questions/752658/is-the-practice-of-returning-a-c-reference-variable-evil
const int& getInt2() {
const int& t = getInt();
cout<<"t = "<<t<<endl;
return t;
}
int main() {
const int& a = getInt2();
cout<<"a = "<<a<<endl;
}
// Output
// t = -10
// a = 1 // Note that a is corrupted
/*
Same thing happens with shared pointer:-
#include <iostream>
#include <memory>
using namespace std;
typedef shared_ptr<int> SharedInt;
SharedInt getInt() {
int a = -30;
return make_shared<int>(a);
}
const SharedInt& getInt2() {
const SharedInt& t = getInt();
cout<<"t = "<<*t<<endl;
return t;
}
int main() {
const SharedInt& a = getInt2();
cout<<"a = "<<*a<<endl;
}
// Output
// t = -10a = 1 // Note that a is corrupted
*/