Public Group

# Namespace question

This topic is 4764 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

if there is a namespace std1;I declare and define some function and class in global scope. and then I do this way: namespace std1 { void ver(void); class down; class lua; } so these defined in global scope will belong to std1? or to add some class and function as a member of a user-declared namespace,all operation like defination ,declaration ,implementation must done in that namspace?

##### Share on other sites
Quote:
 Original post by luasitdownto add some class and function as a member of a user-declared namespace,all operation like defination ,declaration ,implementation must done in that namspace?

Yes.

##### Share on other sites
I'm a little confused as to what you're asking, but I'll try to say what I understand about using namespaces.

aFile.h
[source}namespace myNamespace{   class aClass   (      public:         aClass();         aClass(int val);         ~aClass();         int publicFunc();      private:         void privateFunc();         int aValue;       );}

Class aClass is in namespace myNamespace. In order to add to that namespace it needs to be referenced. For example.

aFile.cpp
namespace myNamespce{   aClass::aClass() : aValue(o)   {      /* Other stuff if required */   }   aClass::aClass(int val) : aValue(val)   {     /* Other stuff if required */   }   aClass::~aClass()   {      /* delete any heap memory etc. */   }   int aClass::publicFunc()   {      return aValue();   }   void aClass::privateFunc()   {      /* privateFunc stuff */   }}

The declaration and definition of aClass are both in namespace myNamespace. To use them you need to either specify the namespace explicitly:
#include aFile.hint main(int argc, char* argv[]){   myNamespace::aClass myClass = new myNamespace::aClass;}

OR use the using directive:
#include aFile.husing namespace myNamespaceint main(int argc, char* argv[]){   aClass myClass = new aClass;}

Namespaces are very useful in splitting projects up into functional areas. For exampel I have a utility namespace that I put useful functions and template classes...and have namespaces embedded in utility in some cases.

They also allow you to create classes/function that would clash with std class function names. You can write a class called sring in a namespace, mystd::string, and use the namespace to avoid confustion with std::string.

Not too sure If I helped...but hopefully I did..:)

##### Share on other sites
that help but what is this for?
namespace myNamespace{class aClass ;...//other class declaration}

I just see some code just put class declaration in a namespace.

##### Share on other sites
Quote:
 Original post by luasitdownthat help but what is this for?*** Source Snippet Removed ***I just see some code just put class declaration in a namespace.

That is called a forward declaration. It tells the compiler that aClass is a class that will be fully defined later.

An example:

class A;  // if you remove this, the compiler will cryclass B{   A a;   // undefined identifier "A".};class A{};

1. 1
2. 2
3. 3
frob
14
4. 4
5. 5
Rutin
12

• 12
• 9
• 57
• 14
• 15
• ### Forum Statistics

• Total Topics
632112
• Total Posts
3004176

×