🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Namespaces

Started by
6 comments, last by Yanroy 24 years, 5 months ago
What are namespaces for? I have a simple idea... just a way to organize your variables? I was wondering if they can also have functions in them, and whether those functions would be defined as if they were part of a class. (i.e. void ClassName::FunctionName(), with ClassName the name of the namespace instead.) Can you make instances of namespaces or include them in other namespaces/classes? Also, what is the using namespace std line at the begining of all my programs? That is what prompted me to look through some headers... it seems to me that namespace std is defined about 10 different times, but I never get an error saying it has been redefined... www.trak.to/rdp

Yanroy@usa.com

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

Advertisement
Namespaces aren't defined per se, you just say what namespace your functions are going into. So you don't create instances, or need to include them. If I want my functions to be considered part of the std namespace I'll wrap all my functions in a namespace std block. To use a function in a different namespace I use std:: or whatever to preface the function call.

Mostly namespaces are for large projects where they don't want helper functions colliding. You can achieve a similar effect with classses and static functions, but it's not good form.

ex:

namespace std {
void HugCow(void);
}

namespace cow {
void HugCow(void);

void MooCow(void) {
std::HugCow();
HugCow();
}

}

Edited by - SiCrane on 1/27/00 10:12:33 AM
If i recall, you cannot add anymore fucntions/memebers to the std namespace. It''s sorta ''finalized and locked''
Nope, actually the std namespace is the default namespace where your functions go if you don''t specify a namespace.

ex: This code compiles and returns 1, at least under gcc version 2.95.1 19990816 (release):

#include <stdio.h>

int HugCow(void) {
return 0;
}

int main(int argc, char ** argv) {
return std::HugCow();
}
Sorry, but that''s not true Si. std is a normal namespace, just like any other, it just happens to be where the standard mandates that all of the standard library goes. And if you write something like you did, it goes in global scope, most definitely not std. (C++ Std: 3.3.5, paragraph 3). Putting things in std by default would defeat the entire purpose. (:

-Brian
GCC must automatically put in a

"using namespace std"

line for you. Hmm, interesting.

Mason McCuskey
Spin Studios - home of Quaternion, 2000 GDC Indie Games Fest Finalist!
www.spin-studios.com
Founder, Cuttlefish Industries
The Cuttlefish Engine lets anyone develop great games for iPad, iPhone, Android, WP7, the web, and more!
Just to clear up the original question here, namespaces help eliminate name conflicts over different groups of variables and functions and such.

For example, say you are using Foo SDK and Foo2 SDK. Well, say that both SDKs have a function with the same signiture. When you tried to compile or link the program, you would get some sort of redefinition error.

Now if each SDK had it's own namespace, this wouldn't happen (unless the namespace was the same and that would just suck).

You can also create your own namespaces as needed, just don't go overboard here. It's a good feature, but too many namespaces will only hurt the readability of your code.

Edited by - I-Shaolin on 1/28/00 4:55:12 PM
Actually, the last time I checked, gcc (in an effort to not offend people that weren''t used to using namespaces) did the equivalent of:

#define std

somewhere. (Actually in the compiler, though.) This made sure that anyone writing std::cout would have working code, and they could slowly move libstdc++ into std without breaking everything. Ultimately, its just a nasty hack, though.

-Brian

This topic is closed to new replies.

Advertisement