Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: using namespace in xcode and dev-c++



I've never seen this usage and it seems very strange as well as completely at odds with what a namespace is.

The usual technique for including files only once is:

[in the header file]
#ifndef GLOBALSTUFF_H
	#define GLOBALSTUFF_H
	...
	(stuff to include)
	...
#endif

[in the .cpp file]
#include "globalstuff.h"
...
(source)
...

Some compilers provide nonstandard alternatives to #include which prevent the same file being included more than once and eliminate the need for the #ifndef, #define and #endif in the header. In objective C/C++, #import is a standard directive which does just that.

A namespace is a different animal.  When you have a structure like this:

namespace globalstuff
{
	...
}

Anything declared inside will have globalstuff:: prepended to its name. To reference a function f declared in the namespace, you would have to use globalstuff::f(), unless you also had:

using namespace globalstuff;

In that case everything in the namespace globalstuff is brought out and made accessible in the current namespace without explicitly specifying it. You might still have to if some names are hidden by other names already visible in the namespace, or something like that. The function of a namespace is not to include headers only once, it is to avoid name collisions.

Maybe some compilers you used had some 'smart' feature which guessed that you really wanted namespace globalstuff and just ignored the '.h'. I can't say, but the name of the namespace should be the same in the using directive and the namespace declaration or it really doesn't make sense. Also, a namespace really should not prevent the reading of a header file multiple times. I might even guess that it wasn't before and some combination of multiple reading and nonstandard interpretation somehow caused the code to work when it shouldn't have.

On Mar 30, 2006, at 10:23 AM, Jeffery Shipman wrote:

I am trying to hammer out a couple differences between compilers. I use xcode and dev-c++. The use of namespace is different between the two compilers.

In Dev-C++ you may put the following in a .h or .cpp file

#include "globalstuff.h"
using namespace globalstuff.h;

In the associated header is the following

#include <cstdlib>
#include <iostream>...

namspace globalstuff
{
#include "node.h"
#include "node.cpp" ..
}

This avoids duplication of headers and libraries. This works fine in Dev-C++ but produces errors in xcode (The .cpp file does not see it's associated .h file). My readings in my class textbooks suggests this should work. Does anyone know why it does not work in xcode.


Thanks

Jeffery Shipman
email@hidden



_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/xcode-users/ email@hidden


This email sent to email@hidden



_______________________________________________ Do not post admin requests to the list. They will be ignored. Xcode-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/xcode-users/email@hidden

This email sent to email@hidden
References: 
 >using namespace in xcode and dev-c++ (From: Jeffery Shipman <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.