File tree Expand file tree Collapse file tree 5 files changed +95
-0
lines changed Expand file tree Collapse file tree 5 files changed +95
-0
lines changed Original file line number Diff line number Diff line change 1+ {
2+ "blurb" : " Code can be organized in namespaces for a clearer structure." ,
3+ "authors" : [
4+ " vaeng"
5+ ],
6+ "contributors" : [
7+ ]
8+ }
Original file line number Diff line number Diff line change 1+ # About
2+
3+ An important method for code organization are namespaces.
4+ Two functions might have a naming collision, which can be resolved by putting them in different namespaces.
5+ Namespaces can be nested, which might help to structure big code bases.
6+ Access to the namespaces is done via the scope-resolution operator ` :: ` .
7+
8+ The example below shows the use of two different ` foo ` functions.
9+ They are used together by prefixing their respective namespaces.
10+
11+ ``` cpp
12+ namespace my_ns {
13+ int foo() {
14+ return 44;
15+ }
16+ namespace my_inner_ns {
17+ int baz() {
18+ return 90;
19+ }
20+ }
21+ }
22+ namespace my_other_ns {
23+ int foo() {
24+ return -2;
25+ }
26+ }
27+ int myresult{my_ns::foo() + my_other_ns::foo() * my_ns::my_inner_ns::baz()};
28+ ```
29+
30+ ~~~~exercism/advanced
31+ Deeply nested namespaces might be too verbose.
32+ It is possible to remove the verbosity with a namespace import via `using`.
33+ This moves all names into the global namespace.
34+ To keep some differentiation aliases might be useful.
35+
36+ ```cpp
37+ int my_other_result{my_ns::my_inner_ns::baz()};
38+
39+ using namespace my_ns::my_inner_ns; // importing the complete namespace
40+ int also_other_result{baz()};
41+
42+ namespace m = my_ns; // setting an alias
43+ namespace o = my_other_ns;
44+
45+ int also_my_result{m::foo() + o::foo() * m::my_inner_ns::baz()};
46+ ```
47+ ~~~~
Original file line number Diff line number Diff line change 1+ # Introduction
2+
3+ An important method for code organization are namespaces.
4+ Two functions might have a naming collision, which can be resolved by putting them in different namespaces.
5+ Namespaces can be nested, which might help to structure big code bases.
6+ Access to the namespaces is done via the scope-resolution operator ` :: ` .
7+
8+ The example below shows the use of two different ` foo ` functions.
9+ They are used together by prefixing their respective namespaces.
10+
11+ ``` cpp
12+ namespace my_ns {
13+ int foo() {
14+ return 44;
15+ }
16+ namespace my_inner_ns {
17+ int baz() {
18+ return 90;
19+ }
20+ }
21+ }
22+ namespace my_other_ns {
23+ int foo() {
24+ return -2;
25+ }
26+ }
27+
28+ int myresult{my_ns::foo() + my_other_ns::foo() * my_ns::my_inner_ns::baz()};
29+ ```
Original file line number Diff line number Diff line change 1+ [
2+ {
3+ "url" : " https://en.cppreference.com/w/cpp/language/namespace" ,
4+ "description" : " C++ reference on namespaces"
5+ }
6+ ]
Original file line number Diff line number Diff line change 827827 "uuid" : " e52453a5-4997-4eba-b754-5bda7b97c701" ,
828828 "slug" : " basics" ,
829829 "name" : " Basics"
830+ },
831+ {
832+ "uuid" : " 7b9bcf07-b447-4c18-b838-a7f4167ff521" ,
833+ "slug" : " namespaces" ,
834+ "name" : " Namespaces"
830835 }
831836 ],
832837 "key_features" : [
You can’t perform that action at this time.
0 commit comments