-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18.16.txt
More file actions
93 lines (86 loc) · 2.52 KB
/
18.16.txt
File metadata and controls
93 lines (86 loc) · 2.52 KB
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
-- using declarations --
a)
namespace Exercise {
int ivar = 0;
double dvar = 0;
const int limit = 1000;
}
int ivar = 0;
using Exercise::ivar;
using Exercise::dvar;
using Exercise::limit;
void manip() {
double dvar = 3.1416;
int iobj = limit + 1;
++ivar;
++::ivar;
}
// using declaration of Exercise::ivar is an error, can't declare an alias
// with the same name as ivar, as it is in the current scope
// dvar and limit is added to the global scope as a local alias from the Exercise namespace
// manip declares a variable named dvar, that hides the outer scope's dvar
// iobj is initialized to Exercise::limit + 1
// if we removed the erroneous using declaration, both incrementing of ivar would increment
// the global one
b)
namespace Exercise {
int ivar = 0;
double dvar = 0;
const int limit = 1000;
}
int ivar = 0;
void manip() {
using Exercise::ivar;
using Exercise::dvar;
using Exercise::limit;
double dvar = 3.1416;
int iobj = limit + 1;
++ivar;
++::ivar;
}
// ivar, dvar and limit from the namespace Exercise is put into manip as a local alias
// the declaration of ivar hides the global ivar inside the manip function
// the declaration of dvar after the using declaration of Exercise::dvar would be an error
// ++ivar increments Exercise::ivar
// ++::ivar increments ivar in the global scope
-- using directive --
a)
namespace Exercise {
int ivar = 0;
double dvar = 0;
const int limit = 1000;
}
int ivar = 0;
using namespace Exercise;
void manip() {
double dvar = 3.1416;
int iobj = limit + 1;
++ivar;
++::ivar;
}
// using directive lifts Exercise's members in the nearest scope that contains Exercise and the directive:
// into the global scope
// clash between ::ivar and Exercise::ivar, detected only if ivar is used
// declaration of dvar inside manip hides the dvar inside the global scope
// ++ivar is ambiguous: global ivar, or Exercise::ivar?
// ++::ivar increments the global ivar (::ivar is a qualified name)
b)
namespace Exercise {
int ivar = 0;
double dvar = 0;
const int limit = 1000;
}
int ivar = 0;
void manip() {
using namespace Exercise;
double dvar = 3.1416;
int iobj = limit + 1;
++ivar;
++::ivar;
}
// using directive lifts Exercise's members in the nearest scope that contains Exercise and the directive:
// into the global scope
// clash between ::ivar and Exercise::ivar, detected only if ivar is used
// declaration of dvar inside manip hides the dvar inside the global scope
// ++ivar is ambiguous: global ivar or Exercise::ivar?
// ++::ivar increments the global ivar (::ivar is a qualified name)