Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


question about namespaces


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 noatom   Members   -  Reputation: 785

Like
0Likes
Like

Posted 06 February 2013 - 03:32 PM

Can you use the same namespace in 2 files? For ex:

 

test.h

 

namespace x

{

int i;

};

 

test2.h

namespace x

{

int u;

};

 

I guess you have you have to include test.h in test2 right? I just find this kinda peculiar because untill now I never really used namespaces a lot,and it's weird for me to see you can make definitions in the same namespace in different files.

 

Anyway,am I correct?



Sponsor:

#2 Paradigm Shifter   Crossbones+   -  Reputation: 5412

Like
4Likes
Like

Posted 06 February 2013 - 03:36 PM

Nope, namespaces are "open" in that you can add a name to a namespace in as many different files as you want, and they don't need to include any of the others. All the standard library headers do that.

C# has partial classes which allow you to do the same with a class as well.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#3 noatom   Members   -  Reputation: 785

Like
0Likes
Like

Posted 06 February 2013 - 05:21 PM

wait...so can i add variables and functions to the same namespace from different header files? 



#4 rip-off   Moderators   -  Reputation: 8540

Like
1Likes
Like

Posted 06 February 2013 - 05:45 PM

Yup.



#5 SiCrane   Moderators   -  Reputation: 9630

Like
1Likes
Like

Posted 06 February 2013 - 06:17 PM

There is one exception: it's undefined for a C++ program to add to namespace std with a few exceptions like adding specializations for existing library classes.

#6 Paradigm Shifter   Crossbones+   -  Reputation: 5412

Like
0Likes
Like

Posted 06 February 2013 - 06:24 PM

Downside is you can have headscratching moments whereby you cant add a string as a key on a map even though the compiler knows about a string (forward declaration I guess), but it doesn't know about string::operator< because you didn't #include <string>
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#7 larspensjo   Members   -  Reputation: 1557

Like
0Likes
Like

Posted 07 February 2013 - 06:33 AM

This is useful for forward declarations. For example, in you header file, you can do:

 

namespace XXX {
    class YYY;
};

And then you can use declarations with references to XXX::YYY in your header file, without having to include the header file where the real declaration is.


Current project: Ephenation.
Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS