Use key question

I am well aware of the use of namespaces, but from time to time I come across a use that uses a particular class. For instance:

#include <string> using namespace std; (...) 

However - from time to time I see:

 using std::string; 

How do I interpret “use” in this case?

Greetings

+4
source share
5 answers

using std :: string just imports std :: string into the current scope (otherwise you can just use "string" rather than "std :: string") without importing everything from :: std into the current scope.


edit: clarify after the comment.

+23
source

using namespace foo allows you to access all the names in the foo namespace without qualification. using foo::bar allows you to use bar without qualification, but not any other names in foo.

+12
source

In this case, it allows you to bind to a specific type in the namespace without qualification. Unlike the first case, which allows you to become attached to any type.

+3
source

You can use the string class without putting std :: in front of it. However, if you want to use something else, like a vector, you need to use std::vector

+3
source

To complicate the situation too much, this can be done:

 class Base { protected: void f(); }; class Fun: public Base { public: using Base::f; }; 

and now you have a good public method.

+1
source

All Articles