namespace

Started by
8 comments, last by Forcas 22 years, 8 months ago
Howdah. I recently read about a C++ feature called namespaces. From what I understand, namespaces allow you to avoid making global identifiers that have already been declared in libraries or headers that you program uses. Anyway, here''s the questions: Up until now, I''ve always declared globals and ignored namespaces. I''ve never declared any global identifiers used in the libraries and headers that my program uses. Is this because all of the built-in headers and libraries use namespaces themselves? Do I even need to use namespaces, or are they just for developing libraries and headers. -Forcas "Elvis is alive. He is Barney the purple dinosaur. He is the pied piper that leads our children into the wages of sin and eternal damnation."
-Forcaswriteln("Does this actually work?");
Advertisement
Namespaces just help to put something in a named scope. They reduce the chances of name collision. You might not need them now, or ever. But the bigger your project is, the more likely it is that you''ll find them useful. You can achieve a similar effect to namespaces by adding a prefix to all your identifier names, but that means you end up with long and potentially unwieldy identifiers. By contrast, a namespace lets you resolve potention collisions but still gives you short identifier names when you use the ''using'' keyword.
most of the standard libraries and headers use the std namespace. you may have seen some code with the line

using namespace std;

at the top. that sets the scope of your program to the standard namespace. just as an easy example, you could make your own header file (call it whatever you want) and have the following.

namespace myConstants {
const double PI = 3.14159;
....
} // end namespace

then in your main program (or anywhere else you include the header) you can access whatever is in that namespace by the following.

double areaOfCircle = myConstants :: PI * radius * radius;

hope this helps.

cyn

Edited by - cyn on August 6, 2001 1:35:44 AM
I''m confused about using "using namespace".

If I do a using namespace, then I can use identifiers in that namespace without using a scope operator. How does the compiler (or the coder) tell the difference between an identifier inside the namespace and the identifier outside the namespace? Are the using namespace keywords useful?

int counter;

namespace MyNameSpace
{
int counter;
}

using namespace MyNameSpace;

void main()
{
counter = 10; // How does it know which counter to use?
}



-Forcas

"Elvis is alive. He is Barney the purple dinosaur. He is the pied piper that leads our children into the wages of sin and eternal damnation."



-Forcaswriteln("Does this actually work?");
*bump*
-Forcaswriteln("Does this actually work?");
quote:Original post by Forcas
If I do a using namespace, then I can use identifiers in that namespace without using a scope operator. How does the compiler (or the coder) tell the difference between an identifier inside the namespace and the identifier outside the namespace? Are the using namespace keywords useful ?

Instead of waiting for someone to answer, why didn''t you just try it? (It should be ''int main'', btw.)

error C2872: ''counter'' : ambiguous symbol

The answer is, it can''t tell the difference. You only use ''using namespace'' when you know it''ll be ok. It''s an option that can make your life easier, but won''t suit all situations. If it was possible to put all symbols from all namespaces together in one namespace with no collisions, there''d be no point having the namespace in the first place.

You can use individual parts of the namespace with the using keyword too, for example:
#include <iostream>using std::cout;using std::endl;int main(){    cout << "hello" << endl;    return 1;} 


I know I could've have tried it. So what's the whole purpose of using namespace, then? Isn't using name space just like having a bunch of global identifiers?

Oh yeah.... and why does your main function need to return an int?

-Forcas

"Elvis is alive. He is Barney the purple dinosaur. He is the pied piper that leads our children into the wages of sin and eternal damnation."





Edited by - Forcas on August 6, 2001 12:56:59 AM
-Forcaswriteln("Does this actually work?");
Namespaces are about grouping things together. You can (but you don''t have to) use namespaces to put classes and other definitions that belong together in one logical unit. And an added advantage is indeed that you have less chance of name collisions, but it''s still up to the programmer to know what''s good and what''s possible (and what''s not). So it''s not really something that will solve all your problems , but more something that makes the application design more robust and more readable, and as such might encourage code reuse.

And it''s int main because the main should return a value that is used by the operating system (or the calling application) to know whether your application terminated succesfully, or if there was something wrong. The ANSI standard (I think) says that functions should return 0 if everything is ok, and nonzero is there was a problem.
If you use void main, the operating system will get some random number (the number stored in the EAX register at the time of termination, I think), and if it is non-zero, it might assume that there was an error and bug the user with it.

Dormeur
Wout "Dormeur" NeirynckThe Delta Quadrant Development Page
quote:Original post by Forcas
So what''s the whole purpose of using namespace, then? Isn''t using name space just like having a bunch of global identifiers?

A global is global in all files. But a ''using namespace'' directive only makes the identifiers in the namespace ''global'' in that file. So in files where there''s no chance of naming collisions, you can use ''using namespace'' to make your identifiers shorter to type, and in files where there is a chance of naming collisions, you specify the object''s scope precisely, as in std::whatever.

quote:Oh yeah.... and why does your main function need to return an int?

Because that''s what the standard says. One way to look at it is that programs are ''functions'' that the operating system calls. The operating system expects a return value to say whether the program succeeded or not. Visual C++ lets you get away without declaring this, but one day you might have to write a program on a different compiler for a different system and it won''t work.
you may also want to consider that the use of "using namespace" is not compulsory, so you could use

int counternamespace mynamespace{    int counter;}int main{    mynamespace::counter++;    return 0;} 


with no drama. the other place where namespaces are used implicitly is when you declare static functions in a class.


--


Games, Anime and more at GKWorld

Meet Bunny luv'' and the girls, all ready to perform on your desktop just for you (warning: adult content).

This topic is closed to new replies.

Advertisement