forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex18.15.16.17.cpp
224 lines (184 loc) · 6.72 KB
/
ex18.15.16.17.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/***************************************************************************
* @file main.cpp
* @author Queequeg
* @date 20 Nov 2014
* @remark This code is for the exercises from C++ Primer 5th Edition
* @note
***************************************************************************/
//
// Exercise 18.15
// Explain the differences between using declarations and directives.
// This difference in scope between a using declaration and a using directive
// stems directly from how these two facilities work. In the case of a using
// declaration, we are simply making name directly accessible in the local
// scope. In contrast, a using directive makes the entire contents of a
// namespace available In general, a namespace might include definitions that
// cannot appear in a local scope. As a consequence, a using directive is
// treated as if it appeared in the nearest enclosing namespace scope.
// Exercise 18.16
// Explain the following code assuming using declarations for all the
// members of namespace Exercise are located at the location labeled
// position 1. What if they appear at position 2 instead? Now answer the
// same question but replace the using declarations with a using directive
// for namespace Exercise.
// Exercise 18.17
// Write code to test your answers to the previous question.
#include <iostream>
using std::cout;
using std::endl;
// using declarations for all the members of namespace Exercise
// are located at the location labeled position 1.
namespace Test0
{
namespace Exercise
{
int ivar = 0;
double dvar = 0;
const int limit = 1000;
}
int ivar = 0;
// using Exercise::ivar;
// error C2874: using-declaration causes a multiple declaration of 'Test0::Exercise::ivar'
// So we delete it to make the program compile.
using Exercise::dvar;
using Exercise::limit;
void manip()
{
double dvar = 3.1416;
cout << "********** Before call Test0::manip **********" << endl;
cout << "Exercise::ivar " << Exercise::ivar << endl
<< "Exercise::dvar " << Exercise::dvar << endl
<< "Exercise::limit " << Exercise::limit <<endl
<< "Test0::ivar " << Test0::ivar << endl
<< "dvar in function manip" << dvar << endl;
int iobj = limit + 1; // Exercise::limit + 1
++ivar; // ++ Test0::ivar
++Test0::ivar; // ++ Test0::ivar
cout << "********** After call Test0::manip **********" << endl;
cout << "Exercise::ivar " << Exercise::ivar << endl
<< "Exercise::dvar " << Exercise::dvar << endl
<< "Exercise::limit " << Exercise::limit <<endl
<< "Test0::ivar " << Test0::ivar << endl
<< "dvar in function manip " << dvar << endl
<< "iboj in function manip " << iobj << endl;
}
}
// using declarations for all the members of namespace Exercise
// are located at the location labeled position 2.
namespace Test1
{
namespace Exercise
{
int ivar = 0;
double dvar = 0;
const int limit = 1000;
}
int ivar = 0;
void manip()
{
using Exercise::ivar;
// using Exercise::dvar;
// This using declaration causes a redefinition of Test1::Exercise::dvar.
// So we delete it to make the program compile.
using Exercise::limit;
double dvar = 3.1416;
cout << "********** Before call Test1::manip **********" << endl;
cout << "Exercise::ivar " << Exercise::ivar << endl
<< "Exercise::dvar " << Exercise::dvar << endl
<< "Exercise::limit " << Exercise::limit <<endl
<< "Test1::ivar " << Test1::ivar << endl
<< "dvar in function manip" << dvar << endl;
int iobj = limit + 1; // Exercise::limit + 1
++ivar; // ++ Exercise::ivar
++Test1::ivar; // ++ Test1::ivar
cout << "********** After call Test1::manip **********" << endl;
cout << "Exercise::ivar " << Exercise::ivar << endl
<< "Exercise::dvar " << Exercise::dvar << endl
<< "Exercise::limit " << Exercise::limit <<endl
<< "Test1::ivar " << Test1::ivar << endl
<< "dvar in function manip " << dvar << endl
<< "iboj in function manip " << iobj << endl;
}
}
// using directive for namespace Exercise is located at the
// location labeled position 1.
namespace Test2
{
namespace Exercise
{
int ivar = 0;
double dvar = 0;
const int limit = 1000;
}
int ivar = 0;
using namespace Exercise;
void manip()
{
double dvar = 3.1416;
cout << "********** Before call Test2::manip **********" << endl;
cout << "Exercise::ivar " << Exercise::ivar << endl
<< "Exercise::dvar " << Exercise::dvar << endl
<< "Exercise::limit " << Exercise::limit <<endl
<< "Test2::ivar " << Test2::ivar << endl
<< "dvar in function manip" << dvar << endl;
int iobj = limit + 1; // Exercise::limit + 1
// ++ivar;
// error C2872: 'ivar' : ambiguous symbol
// could be int Test2::ivar or int Test2::Exercise::ivar
++Test2::ivar; // ++ Test2::ivar
cout << "********** After call Test2::manip **********" << endl;
cout << "Exercise::ivar " << Exercise::ivar << endl
<< "Exercise::dvar " << Exercise::dvar << endl
<< "Exercise::limit " << Exercise::limit <<endl
<< "Test2::ivar " << Test2::ivar << endl
<< "dvar in function manip " << dvar << endl
<< "iboj in function manip " << iobj << endl;
}
}
// using directive for namespace Exercise is located at the
// location labeled position 2.
namespace Test3
{
namespace Exercise
{
int ivar = 0;
double dvar = 0;
const int limit = 1000;
}
int ivar = 0;
void manip()
{
using namespace Exercise;
double dvar = 3.1416;
cout << "********** Before call Test3::manip **********" << endl;
cout << "Exercise::ivar " << Exercise::ivar << endl
<< "Exercise::dvar " << Exercise::dvar << endl
<< "Exercise::limit " << Exercise::limit <<endl
<< "Test3::ivar " << Test3::ivar << endl
<< "dvar in function manip" << dvar << endl;
int iobj = limit + 1; // Exercise::limit + 1
// ++ivar;
// error C2872: 'ivar' : ambiguous symbol
// could be int Test3::ivar or int Test3::Exercise::ivar
++Test3::ivar; // ++ Test3::ivar
cout << "********** After call Test3::manip **********" << endl;
cout << "Exercise::ivar " << Exercise::ivar << endl
<< "Exercise::dvar " << Exercise::dvar << endl
<< "Exercise::limit " << Exercise::limit <<endl
<< "Test3::ivar " << Test3::ivar << endl
<< "dvar in function manip " << dvar << endl
<< "iboj in function manip " << iobj << endl;
}
}
int main()
{
Test0::manip();
cout << endl;
Test1::manip();
cout << endl;
Test2::manip();
cout << endl;
Test3::manip();
cout << endl;
return 0;
}