-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnamespaces.cpp
More file actions
89 lines (48 loc) · 1.98 KB
/
namespaces.cpp
File metadata and controls
89 lines (48 loc) · 1.98 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
#include <iostream>
#include <string>
#include "namespaces.h" // to define the fwd dec for sharing const var
void print(std::string a);
int climate = 9999;
//if you want to share this, then fwd dec with extern before this definition. if it is header, then include that header here too
const int ambulanceNumber = 1111;
extern const int policeNumber = 100; // Valid but not best practice. pls avoid both extern and definition for a global variable at same time
extern int constexpr phnNum_3 = 3333;
namespace
{
//now whatever inside this UNNAMED namespace becomes INTERNAL LINKAGE
int sharedVar = 17;
}
inline void func(int* ptr) {
*ptr = 10;
}
void NScppPrint()
{
//std::cout << "NScppPrint() climate - " << climate << std::endl;
std::cout << "(" << &phnNum << ") " << "namespaces.cpp static var : phnNum = " << phnNum << "\n";
std::cout << "(" << &phnNum_2 << ") " << "namespaces.cpp inline var : phnNum_2 = " << phnNum_2 << "\n";
std::cout << "(" << &phnNum_3 << ") " << "namespaces.cpp const var : phnNum_3 = " << phnNum_3 << "\n";
std::cout << "(" << &avogadro << ") " << "namespaces.cpp constexpr var : avogadro = " << avogadro << "\n";
std::cout << "(" << &myGravity << ") " << "namespaces.cpp inline constexpr var : myGravity = " << myGravity << "\n\n\n";
//std::cout << "NScppPrint() ambulanceNumber - " << ::ambulanceNumber << std::endl;
}
int printVanakam()
{
std::cout << "Vanakam" << std::endl;
return 0;
}
//any change here only recompile this .cpp's translation unit
namespace Global_constants_as_external_variables
{
// We use extern to ensure these have external linkage
extern constexpr double pi{ 3.14159 };
extern constexpr double avogadro{ 6.0221413e23 };
extern constexpr double myGravity{ 9.2 }; // m/s^2 -- gravity is light on this planet
}
void print(std::string a)
{
std::cout << "global print namespace says '" << a << "'" << '\n';
}
void printMyName()
{
std::cout << "My name is Logesh from 'global' namespace\n";
}