Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

style and namespaces [duplicate]

Possible Duplicate:
Why is 'using namespace std;' considered a bad practice in C++?

I've seen some code examples where people use, say, std::cout whereas in other places people will have using namespace std; at the top for simplicity instead. Which is generally preferred?

like image 495
Emir Avatar asked Aug 15 '12 14:08

Emir


People also ask

What do you mean by namespace?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. 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.

What is namespace in C sharp?

The namespace keyword is used to declare a scope that contains a set of related objects. You can use a namespace to organize code elements and to create globally unique types.

What is namespace in dot net?

Namespaces are used to organize the classes. It helps to control the scope of methods and classes in larger . Net programming projects. In simpler words you can say that it provides a way to keep one set of names(like class names) different from other sets of names.

Should Typedef be in namespace?

You may place it almost anywhere that you could otherwise place a preprocessing directive. A typedef can go either outside a namespace (which makes it global and causes it to apply everywhere), or inside (which causes it to be scoped to that namespace).


2 Answers

Use std::cout to avoid any potential name clashes. If you use using using namespace std; you will populate your global namespace with all the std name which might conflict with classes or function names you or someone else in your team have written. This is well explained in the C++ faq lite and in SO

like image 199
tiguero Avatar answered Sep 20 '22 09:09

tiguero


I personally use the full namespace denomination when writing code, e.g. std::string, etc. It makes things clearer for anyone who reads that, which function the developer wants to use.

I've seen the following saying:

write it once, read it a thousand times...

:)

like image 42
fduff Avatar answered Sep 20 '22 09:09

fduff