Im so proud of myself!!

Started by
10 comments, last by Zahlman 19 years ago
Excellent. Keep up the good work!
Without order nothing can exist - without chaos nothing can evolve.
Advertisement
Quote:Original post by dustydoodoo
Hmmmm, i kinda wrote that pretty fast, ussually i do just put if(stuffhappens)or
if (!stuffhappens), so, yay, and i could someone tell me what std is for? or means, i know its other meaning, lol, but what is its programing meaning? because the tutorial i learned it from, didnt have that in it.


std is short for "standard", and it's the name of a namespace. Namespaces are a tool that C++ provides for organizing definitions (and globals) so that you can avoid naming collisions while still not having to do weird "name mangling" all the time.

The usual example that is used deals with std::vector: in game programming, or anything graphical for that matter, it is common to create types with two or three numeric values and call them "vectors" - which they are, in the mathematical sense. Were it not for the namespacing, you'd have a problem if you wanted to use the library 'vector' container and your own 'vector' class (for the mathematical entity) in the same project: basically, you'd have to rename one of them (yours, since you don't get to pick names for stuff in the standard library).

In the C days, this 'renaming' would usually consist of putting some prefix on each identifier, which is annoying and somewhat error prone - you have to remember which one you want. The namespace concept gives you the flexibility to avoid typing the "prefixes" all over the place (via a using statement), but still allows you to make your intention clear when there would be a conflict:

{  using namespace std;  // everything in this block that could be interpreted as something in the std  // namespace, is.}{  using std::vector;  // inside this block "vector" means std::vector, but e.g. "string" means your  // own string class, not std::string.}{  // inside this block, "vector" means your struct that represents mathematical  // vectors, not std::vector. If you need to use a std::vector, you can still  // get it by typing out std::vector in full.  {    using namespace std;    // Of course, scopes nest, and 'using' declarations follow the same scoping    // rules as local variables: thus in the inner block, we are using namespace    // std, but after the close brace...  }  // here, we no longer use namespace std.}


One final note: Avoid using declarations, and especially do not use full namespaces, in your header files. This is because you will probably be putting them at file scope, thus they will basically affect everything in every file that includes them... a debugging nightmare when a name collision happens.

This topic is closed to new replies.

Advertisement