I know what you mean. I usually hide the source code and only show the user the program. I make the code look really big and complicated when I'm trying to get a friend to start on C++ from HTML.
I know what you mean. I usually hide the source code and only show the user the program. I make the code look really big and complicated when I'm trying to get a friend to start on C++ from HTML.
Adam
namespaces are useful if you make a function library. Their purpose is to reduce name clashes. Also it helps a human reader understand what a function or variable is for if they are grouped together under a common name. i.e all graphics functions could be grouped under a gfx namespace...
Instead ofCode:namespace gfx { void Initialize(); void Render(); void Shutdown; }
Of course you wouldn't make the gfx scope globle because Initialize, Shutdown and Render are popular names for functions. So you would use the scope operator.Code:void gfx_Initialize(); void gfx_Render(); void gfx_Shutdown();
When calling a function.Code:gfx::Initialize();
Namespaces are open, i.e.
You can keep adding names whenever you want.Code:namespace bob { int x, y; } namespace bob { std::string name; } namespace bob { int age; }
This is the explanation I plan to put in my tutorial. I suppose this is as good a time as any to test its understandability. (Opinions, please!)
No mention of using statements or declarations yet. That'll come later in the tutorial, as I want to start out with good practice.std::cout is kind of a weird name, what with the double colon in the middle. You'll also notice that I kept referring to the object as just cout in the previous section.
There are two kinds of names - the technical term is identifier - in C++. Local names, which are simple, like cout. And global names, which in their full form look like this:
::path::to::identifier
However, the compiler is capable of guessing at parts of this construct, so you can usually get away with not using the full name. For example, it is very often possible to leave out the initial ::.
So what is this all about?
Good identifiers are not available in unlimited numbers. At some point, two people might want to give their objects the same name. And if these two people then go on to combine their code into one program, they have a problem. This problem even has a name: it's called name collision.
The usual solution to name collision is to make the names longer. Most C libraries tend to have a common prefix for everything they provide. For instance, the zlib compression library prefixes everything with a single z. The jpeglib image compression library prefixes everything with jpeg_.
This works. However, it can become tedious to write the prefix over and over, even when you know that you don't need it in the code you're writing.
Namespaces are the answer. The idea is to use a language construct to automatically give all identifiers from a library a common prefix. Since the compiler knows what part is the prefix, it is possible to omit it under some circumstances. We will discuss these circumstances later.
The entire C++ standard library uses the prefix std. cout therefore is the local name of the object within the std namespace, referred to as ::std::cout or, because the initial :: usually can be omitted, std::cout.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
could just include stdafx.h and never have to type std:: ever
ok, let me see if I got this right...
the std namespace is part of the <iostream>...
and it's optional if I use :: or not... no matter how many namespaces I have included?
"I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe
http://www.Bloodware.net - Developing free software for the community.
it works for meCode:#include <iostream> int main() { cout <<"Hello World!\n"; return 0; }
You dont need namespace someone should of just told him this in the first place
You do need the namespace, that code does not work on most compilers and is illegal C++. If it works for you, fine, but you are picking up a bad habit that will screw things up when you switch compilers.
Last edited by Daved; 09-16-2005 at 12:01 PM.
Devil Panther,
Ignore the comment about :: for now. CornedBee was talking about the first :: in ::std::cout. You cannot ignore the middle :: in std::cout.
The std namespace is used by all standard library headers in C++. This includes <iostream>, <fstream>, <string>, <vector>, <cstdlib>, <cstdio>, <cmath>, and many others. To use the things from those headers, you have to tell the compiler that the thing you are using is from the std namespace. There are different ways of doing this.
One is to tell the compiler to always check the std namespace for any name you use (this is the using namespace std; option).
Another is to tell the compiler that any time you use a specific name, to check the std namespace (this is the using std::cout; option).
Another is to tell the compiler every single time that the name you are using is from the std namespace (this is the std::cout option).
There are different ways to use those options as well. The point is that standard library names live in the std namespace, and if you want to use one of them you have to tell the compiler to look there for them.
what compiler do you use?!!! since you do need it with the advance ones!!! it's not Dev C++ that includes things without you knowing!Originally Posted by HaVoX
"I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe
http://www.Bloodware.net - Developing free software for the community.
1) i'm not really sure about the difference between the last two.Originally Posted by Daved
2) so you're saying that the above includes use the std...
3) what's with the ::std::cout ?
thank you.
"I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe
http://www.Bloodware.net - Developing free software for the community.
example 1
example 2Code:using namespace std; // pull in everyting from namespace std cout << i << endl; // no std:: needed because compiler looks for everything in namespace std cin >> i; // like above
example3Code:using std::cout; // pull in only cout from namespace std cout << i << std::endl; // no std:: needed for cout because compiler looks for cout std::cin >> i; // std:: is needed because compiler looks only for cout
KurtCode:// no using declaration std::cout << i << std::endl; // std:: needed because nothing is pulled in std::cin >> i; // like above
3) Ignore the ::std::cout. Basically there is a global namespace that you access with the first ::, but rarely do you need to worry about it.
2) Yes. In C++, all standard library headers put all the names into the std namespace. There are lots of standard library headers besides the examples I gave.
1) Here are examples of the three options I mentioned:Code:#include <iostream> using namespace std; int main() { cout << "Hello World" << endl; return 0; }Code:#include <iostream> using std::cout; using std::endl; int main() { cout << "Hello World" << endl; return 0; }Code:#include <iostream> int main() { std::cout << "Hello World" << std::endl; return 0; }
I see... but why std what does it mean? Can I write things to address the std ?
"I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe
http://www.Bloodware.net - Developing free software for the community.
Did you read the FAQ on namespaces? Did you understand CornedBee's explanation of a namespace above? std is just a namespace. The people who standardized the language decided to put the standard library stuff into a namespace, and they decided to call it std (for standard).
You'll have to do some reading elsewhere if the explanations in this thread aren't enough to explain namespaces.
std is the standard functions.
Let me simplify even further - and use VB to explain.
In VB, I might have a function that is native to the language such as Open for opening files for read/write. In VB, if I type in Open, open, OPEN, OpEn, or any combination of cased letters, the VB IDE would correct the case to Open and now that it's a restricted keyword. In debugging a VB application, if I try to step into the line that says Open, I don't see anything. The Open word doesn't take me to a deeper level.
C++ is case sensitive. Restricted keywords are not auto-corrected for case. IF and if are viewed as two different objects, the first being some sort of variable and the latter is part of an if/then statement. If I am stepping through a C++ program and come upon specific "keywords," the debugger will take me to definitions of those words in their original header files. (note, I might be a bit off in my description but conceptually it should be close.)
std is a huge namespace of common functions/processes typically used in a c++ program. HOWEVER, the "power" of c++ is the compiler only grabs what is absolutely necessary to run the program. VB, on the otherhand, says grab anything that COULD be needed.