diff options
Diffstat (limited to '')
-rw-r--r-- | namespaces.cpp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/namespaces.cpp b/namespaces.cpp new file mode 100644 index 0000000..2e93b61 --- /dev/null +++ b/namespaces.cpp @@ -0,0 +1,37 @@ +#include <iostream> + +#include "namespaces.hpp" + +// to access a namespace the notation is `<namespace>::<element>' +// it is possible to create nested namespaces (!) `<nsA>::<nsB>::elm' +int math::dot(math::vec3i v, math::vec3i w) { + return v.x * w.x + v.y * w.y + v.z * w.z; +} + +// alternatively (but not recommended) we can enclose the code +// in a namespace declaration +namespace math { + double dot(vec3d v, vec3d w) { + return v.x * w.x + v.y * w.y + v.z * w.z; + } +} + + +int main(int argc, char *argv[]) { + + math::vec3i a = { 1, 2, 3 }; + math::vec3i b = { 1, 2, 3 }; + std::cout << math::dot(a, b) << std::endl; + + // it is also possible to specify to implicitly + // always use the namespace in a scope + { + using namespace math; + + vec3d c = { .5, .2, 11.2 }; + vec3d d = { .2, .2, 12.3 }; + std::cout << dot(c, d) << std::endl; + } + + return 0; +} |