-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconst_test.cc
65 lines (52 loc) · 1.07 KB
/
const_test.cc
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
57
58
59
60
61
62
63
64
65
#include <stdio.h>
/*
template<class T>
const T& GetMax(const T& t1, const T& t2) //error: main::GetMax(a, b) error: assignment of read-only location ¡®GetMax<int>((*(const int*)(& a)), (*(const int*)(& b)))¡¯
GetMax(a, b) = 3;
{
if (t1 > t2)
{
return t2;
}
// else
return t2;
}
template<class T> //error: invalid initialization of reference of type ¡®int&¡¯ from expression of type ¡®const int¡¯return t2;
T& GetMax(const T& t1, const T& t2)
{
if (t1 > t2)
{
return t2;
}
// else
return t2;
}
template<class T> //error: assignment of read-only location ¡®GetMax<int>((* & a), (* & b))¡¯ GetMax(a, b) = 3;
const T& GetMax(T& t1, T& t2)
{
if (t1 > t2)
{
return t2;
}
// else
return t2;
}
*/
template<class T> //this is good
T& GetMax(T& t1, T& t2)
{
if (t1 > t2)
{
return t2;
}
// else
return t2;
}
int main()
{
int a = 1;
int b = 2;
GetMax(a, b) = 3;
printf("this is a test\n");
return 0;
}