I have been writing programs in C++ or rather have recently started to, but have just gone back to tutorial 1 and have a slightly theoretical question.
I understand that it IS needed but why do we use namespace to start a program. Why can't it just begin with int main { ?
This'll be answered in the soon-to-come updated FAQ, but until then:
Using 'namespace' provides context. A whole lot of functions (e.g.: cin/cout, data containers, algorithms, ...) are available, but "hidden" in the std namespace. If you plan on using the STL vector, you include <vector> and tell the compiler you're using the STL vector (either by "using namespace std;" or by "using std::vector;"). That way, you have full functionality of the STL vector without bothering with namespaces.
Without the namespace limitations, a lot of function names would be "in use". What if you want your own vector class? You'd happily be programming away, only to get very odd errors ("redefinition" and such). You'd google around, discover there is already a class called 'vector', which was included via something else you included, and be forced to rename your class to myOwnVector or something like that. With namespaces, you don't have that problem. Additionally, in the case that you want to use both, you could refer to vector as your own class, and to std::vector for the standard vector.
So, in short, namespaces provide context. Reusing my example from another topic: "I like their latest album" has no meaning if you don't know who "they" is. Either you provide explicit context, "I like The Beatles' last album", or implicitly if the entire conversation is about The Beatles.
Thanks for your response and I'm afraid you will have to excuse my total stupidity :-/ I think I have been ploughing on regardless but am coming unstuck because I haven't got my head around some of these gritty details.
Using 'namespace' provides context. A whole lot of functions (e.g.: cin/cout, data containers, algorithms, ...) are available, but "hidden" in the std namespace.
I have been under the illusion that the functions were included when I included the libraries. I didn't realise that I then needed to tell the compiler that I am using them. I thought that the compiler would know I am using the functions in the libraries simply by including them. I'm not getting totally confused here am I?
That way, you have full functionality of the STL vector without bothering with namespaces.
Very confused :-/
Does the namespace make sure that when I use a class defined in the library, the compiler looks in the library?
Think of it as a way of differentiating between different things with the same name. Imagine a 'pen'. If there is only one, there is no ambiguity; any pen-related operation will know what to use.
Now imagine being in a classroom with 20 people, each with a pen. If I want to steal(pen), there is no way to know what pen I want to steal, because 20 people have a pen, each called 'pen'. I can be explicit by saying "steal(Bob::pen)". If I'm going to need several attempts to steal it, but will only try to attempt to steal Bob's pen, I can clarify "using Bob::pen", so that every reference to 'pen' will refer to 'Bob::pen'. If I intend to steal more of Bob's stuff, but only Bob's stuff, I can generalize by "using namespace Bob". Any item referred to will be assumed to be Bob's item.
Includes, on the other hand, teach you how to use a certain class/item. They are just "hidden" in the std namespace to prevent "collision". Since included files often silently include other files, you often don't know what included and what isn't. If they weren't hidden in the std namespace, you'd have to be very careful with naming conventions.
An example: I want to write a sort function and compare it to the STL's sort. I want an obvious name for my sort, so I call it 'sort'. Now, I include <algorithm> to test the STL's sort. Without namespaces, there are two functions called "sort", so which one will be used? Who knows! With namespaces, however, I can distinguish the two. "sort" is mine, "std::sort" is the STL's sort. If it turns out my sort is terrible, I can delete the sort, type "using std::sort" and all calls to sort will now use std::sort.
Ok I think I'm starting to get it. I just think that in the situations I have dealt with so far there has been no need to worry about it.
Am I right then in thinking that you can have more than one namespace in a single program then? I can understand that with your analogy using Bob and the pen and in fact it is quite clear now.
Is it possible to make a program using no namespaces at all?
#include <iostream> // needed for std::cout
int main()
{
std::cout << "hello world";
}
Is also a valid program.
1 2 3 4 5 6 7 8
#include <iostream> // needed for std::cout
usingnamespace std; // brings std::cout into global namespace so you can refer to it
// as "cout" instead of "std::cout"
int main()
{
cout << "hello world";
}
Okaayyyyy now I'm really starting to get it! Lights switching on! I have was also wondering about the :: when I have seen it in posts. It's all much clearer now
Thanks for your help people. I think this one is solved!