Jump to content
  • Advertisement
Sign in to follow this  
luasitdown

Namespace question

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by luasitdown
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?


Yes.

Share this post


Link to post
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.h

int main(int argc, char* argv[])
{
myNamespace::aClass myClass = new myNamespace::aClass;
}



OR use the using directive:

#include aFile.h

using namespace myNamespace

int 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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by luasitdown
that 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 cry

class B
{
A a; // undefined identifier "A".
};

class A
{
};


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!