Skip to content

Commit c1e980a

Browse files
committed
added progress
1 parent 397bc59 commit c1e980a

11 files changed

+206
-0
lines changed

Chapter18/18.13.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
when needing variables that don't span multiple files and have a static lifetime
2+
or declaring file statics

Chapter18/18.14.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mathLib::MatrixLib::matrix mathLib::MatrixLib::operator*(const matrix &, const matrix &);
2+
3+
// as soon as the fully qualified name (mathLib::MatrixLib::operator*) is seen,
4+
// we are in the scope of that namespace,
5+
// so we can use matrix instead of mathLib::MatrixLib::matrix in the parameters

Chapter18/18.15.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using declarations make only one member of a namespace visible,
2+
it can appear in global, local, namespace or class scope (only to refer to base class members)
3+
declares a local alias for the selected namespace member
4+
immediately throws an error if there is a name clash
5+
6+
using directives make all members of a namespace visible
7+
it can appear in global, local or namespace scope
8+
lifts the namespace members in the nearest scope that contains both the namespace and the using directive
9+
if there is a name clash, throws error when trying to use the clashing name, not immediately

Chapter18/18.16.txt

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
-- using declarations --
2+
a)
3+
namespace Exercise {
4+
int ivar = 0;
5+
double dvar = 0;
6+
const int limit = 1000;
7+
}
8+
9+
int ivar = 0;
10+
using Exercise::ivar;
11+
using Exercise::dvar;
12+
using Exercise::limit;
13+
void manip() {
14+
double dvar = 3.1416;
15+
int iobj = limit + 1;
16+
++ivar;
17+
++::ivar;
18+
}
19+
// using declaration of Exercise::ivar is an error, can't declare an alias
20+
// with the same name as ivar, as it is in the current scope
21+
// dvar and limit is added to the global scope as a local alias from the Exercise namespace
22+
// manip declares a variable named dvar, that hides the outer scope's dvar
23+
// iobj is initialized to Exercise::limit + 1
24+
// if we removed the erroneous using declaration, both incrementing of ivar would increment
25+
// the global one
26+
27+
b)
28+
namespace Exercise {
29+
int ivar = 0;
30+
double dvar = 0;
31+
const int limit = 1000;
32+
}
33+
34+
int ivar = 0;
35+
void manip() {
36+
using Exercise::ivar;
37+
using Exercise::dvar;
38+
using Exercise::limit;
39+
double dvar = 3.1416;
40+
int iobj = limit + 1;
41+
++ivar;
42+
++::ivar;
43+
}
44+
// ivar, dvar and limit from the namespace Exercise is put into manip as a local alias
45+
// the declaration of ivar hides the global ivar inside the manip function
46+
// the declaration of dvar after the using declaration of Exercise::dvar would be an error
47+
// ++ivar increments Exercise::ivar
48+
// ++::ivar increments ivar in the global scope
49+
50+
-- using directive --
51+
a)
52+
namespace Exercise {
53+
int ivar = 0;
54+
double dvar = 0;
55+
const int limit = 1000;
56+
}
57+
58+
int ivar = 0;
59+
using namespace Exercise;
60+
void manip() {
61+
double dvar = 3.1416;
62+
int iobj = limit + 1;
63+
++ivar;
64+
++::ivar;
65+
}
66+
// using directive lifts Exercise's members in the nearest scope that contains Exercise and the directive:
67+
// into the global scope
68+
// clash between ::ivar and Exercise::ivar, detected only if ivar is used
69+
// declaration of dvar inside manip hides the dvar inside the global scope
70+
// ++ivar is ambiguous: global ivar, or Exercise::ivar?
71+
// ++::ivar increments the global ivar (::ivar is a qualified name)
72+
73+
b)
74+
namespace Exercise {
75+
int ivar = 0;
76+
double dvar = 0;
77+
const int limit = 1000;
78+
}
79+
80+
int ivar = 0;
81+
void manip() {
82+
using namespace Exercise;
83+
double dvar = 3.1416;
84+
int iobj = limit + 1;
85+
++ivar;
86+
++::ivar;
87+
}
88+
// using directive lifts Exercise's members in the nearest scope that contains Exercise and the directive:
89+
// into the global scope
90+
// clash between ::ivar and Exercise::ivar, detected only if ivar is used
91+
// declaration of dvar inside manip hides the dvar inside the global scope
92+
// ++ivar is ambiguous: global ivar or Exercise::ivar?
93+
// ++::ivar increments the global ivar (::ivar is a qualified name)

Chapter18/18.17/Exercise.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "Exercise.h"
2+
3+
namespace Exercise {
4+
int ivar = 0;
5+
double dvar = 0;
6+
extern const int limit = 1000;
7+
}

Chapter18/18.17/Exercise.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace Exercise {
2+
extern int ivar;
3+
extern double dvar;
4+
extern const int limit;
5+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "Exercise.h"
2+
3+
int ivar = 0;
4+
using Exercise::ivar; // error: target of using declaration conflicts with declaration already in scope
5+
using Exercise::dvar;
6+
using Exercise::limit;
7+
void manip() {
8+
double dvar = 3.1416;
9+
int iobj = limit + 1;
10+
++ivar;
11+
++::ivar;
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "Exercise.h"
2+
3+
int ivar = 0;
4+
void manip() {
5+
using Exercise::ivar;
6+
using Exercise::dvar;
7+
using Exercise::limit;
8+
double dvar = 3.1416; // error: declaration conflicts with target of using declaration already in scope
9+
int iobj = limit + 1;
10+
++ivar;
11+
++::ivar;
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "Exercise.h"
2+
3+
int ivar = 0;
4+
using namespace Exercise;
5+
void manip() {
6+
double dvar = 3.1416;
7+
int iobj = limit + 1;
8+
++ivar; // error: reference to ivar is ambiguous
9+
++::ivar;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "Exercise.h"
2+
3+
int ivar = 0;
4+
void manip() {
5+
using namespace Exercise;
6+
double dvar = 3.1416;
7+
int iobj = limit + 1;
8+
++ivar; // error: reference to ivar is ambiguous
9+
++::ivar;
10+
}

Chapter18/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,44 @@ Write a program that uses the Sales_data addition operator on objects that have
6161
Why is it important that the what function doesn’t throw?
6262
## [Exercise 18.12](18.12)
6363
Organize the programs you have written to answer the questions in each chapter into their own namespaces. That is, namespace chapter15 would contain code for the Query programs and chapter10 would contain the TextQuery code. Using this structure, compile the Query code examples.
64+
## [Exercise 18.13](18.13.txt)
65+
When might you use an unnamed namespace?
66+
## [Exercise 18.14](18.14.txt)
67+
Suppose we have the following declaration of the operator* that is
68+
a member of the nested namespace mathLib::MatrixLib:
69+
```
70+
namespace mathLib {
71+
namespace MatrixLib {
72+
class matrix { /* ... */ };
73+
matrix operator*
74+
(const matrix &, const matrix &);
75+
// ...
76+
}
77+
}
78+
```
79+
How would you declare this operator in global scope?
80+
## [Exercise 18.15](18.15.txt)
81+
Explain the differences between using declarations and directives.
82+
## [Exercise 18.16](18.16.txt)
83+
Explain the following code assuming using declarations for all the
84+
members of namespace Exercise are located at the location labeled position 1. What
85+
if they appear at position 2 instead? Now answer the same question but replace the
86+
using declarations with a using directive for namespace Exercise.
87+
```
88+
namespace Exercise {
89+
int ivar = 0;
90+
double dvar = 0;
91+
const int limit = 1000;
92+
}
93+
int ivar = 0;
94+
// position 1
95+
void manip() {
96+
// position 2
97+
double dvar = 3.1416;
98+
int iobj = limit + 1;
99+
++ivar;
100+
++::ivar;
101+
}
102+
```
103+
## [Exercise 18.17](18.17)
104+
Write code to test your answers to the previous question.

0 commit comments

Comments
 (0)