In the boost libraries, there are often examples of including the library like:
#pragma once #include <boost/property_tree/ptree.hpp> using boost::property_tree::ptree;
Throughout my program I have been importing namespaces like this:
#include "../MyClass.h" using namespace MyClassNamespace;
Can someone please explain:
using
and using namespace
;using namespace
in favour of using
;using
and using namespace
;Thanks
The using directive is used to use a namespace in your code, without explicitly referencing to a namespace. This is of course useful when using a namespace more than once in a class. The directive does not change the state or status of the class, as the namespace statement does.
Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.
The statement using namespace std is generally considered bad practice. The alternative to this statement is to specify the namespace to which the identifier belongs using the scope operator(::) each time we declare a type. Example 1: CPP.
Simply put #include tells the pre-compiler to simply copy and paste contents of the header file being included to the current translation unit. It is evaluated by the pre-compiler. While using directive directs the compiler to bring the symbol names from another scope in to current scope.
using namespace
makes visible all the names of the namespace, instead stating using
on a specific object of the namespace makes only that object visible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With