How to use one namespace in files?

I have a C ++ project (VC ++ 2008) that uses only the std namespace in many source files, but I can’t find the “right” place to put using namespace std;.

If I put it in main.cpp, it does not seem to extend to my other source files. I worked when I put this in the header file, but have since been told that it is bad. If I put it in all my .cpp files, the compiler does not recognize the std namespace.

How to do it?

+3
source share
6 answers

Usually you have three accepted options:

  • Using scope (std :: Something)

, # 1 - , .

, , , . - , . , , -, .

- , , . , , . , -, , , . .

: (std:: Something), .

+2

, , , ,

#include <list>

class CYourClass
{
    std::list<int> myListOfInts;
    ...
};

cpp

int CYourClass::foo()
{
    std::list<int>::iterator iter = myListOfInts.begin();
    ...
}

" std" . , std:: too much, , " std", . .

int CYourClass::foo()
{
    using namespace std;
    list<int>::iterator iter = myListOfInts.begin();
    ...
}
+2

- .cpp .

0

, :

1) " ", using namespace std cpp

2) using namespace std .

, , , , using namespace std . -, std - "", , .

0

. .

:

std; blah < ;

std:: blah < ;

- , .

"" , , (.. include)

0

It is not recommended to distribute the code using namespace std;just because it is convenient for you. But if you insist on it, you can wrap your own code in this namespace. And only later, when you will actually use your function / classes in the main file, you define using namespace std;. To emphasize what I'm saying, here is an example:

namespace std
{
   class MyNewClass
   {
       public:
             MyNewClass( ) { out << "Hello there" << endl;};
   };
}; 

int main( )
{
    std::MyNewClass tmp;
};
0
source

Source: https://habr.com/ru/post/1711027/


All Articles