forked from chihyang/CPP_Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExer03_11.cpp
More file actions
30 lines (29 loc) · 928 Bytes
/
Copy pathExer03_11.cpp
File metadata and controls
30 lines (29 loc) · 928 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
// Warning: This is for verification. It CANNOT be compiled successfully by every compiler.
// The problem lies in line 17.
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main()
{
const string s = "Keep out!";
// Type of c is const reference(or more precisely, reference to const).
// If no operation in range for is to change value of s, it's okay, or it's an error.
for(auto &c : s)
{
cout << c; // okay
c = 'X'; // error, cannot compile successfully
}
cout << endl;
return 0;
}
// ******compile info under cl******
// Exer03_11.cpp
// Exer03_11.cpp(17) : error C3892: “c”: 不能给常量赋值
// ******compile info under g++******
// Exer03_11.cpp: In function 'int main()':
// Exer03_11.cpp:17:5: error: assignment of read-only reference 'c'
// c = 'X'; // error, cannot compile successfully
// ^