How to layout codes in namespace and library?

I have a question about how I should layout the codes for namespace.
Assume in my code I have three class a, b, c and I want to put them in namespace own
Then I plan to create file ownlib.h and ownlib.cpp

in ownlib.h, I put all three class declarations as:

namespace own
{
class a
{
private:
public : func1
}

class b
{
private:
public : func1
}

class c
{
private:
public : func1
}
}

in ownlib.c,I put three class declarations as:

#include "ownlib.h"

namespace own
{
void a::func1();
void b::func1();
void c::func1();

}

Is this the right layout?

Based my understanding, I can compile these two files (.h and .cpp) to static library to be a ownlib.lib file.
But namespace and library should be two different concepts. So I have following questions:
1. Can I add class D, which is in namespace own, but not in library ownlib? If I can, how?
2. Can I add class D, which is in library ownlib, but not in namespace own? If I can, how?


Thanks in advance.





namespace is what it imply: a naming convention. So you can put the class D in any namespace you want no matter whether that namespace already exists or not.

Yes it is irrelevant if and in what context the namespace appears. Just avoid name conflicts.
What you put in the header file will be visible. The classes,functions,... in the cpp are hidden. So put only in the .Hpp what you want to expose...
Last edited on
Thanks for reply.

Would you please give an example how to write the code that:

putting the class D in library ownlib, but add it to another namespace (e.g.: other)?

Thanks in advance.
Whether the class D appears in the library ownlib depends on your project setting (which depends on the IDE you're using, basically: tell the IDE that you want the file associated with class D in ownlib)

but add it to another namespace (e.g.: other)?
Um
1
2
3
4
5
6
namespace other
{
  class D
  {
  };
}
?
Thanks for reply.

Meanwhile, we can also use a same namespace in different cpp files, as :

in file a.cpp:

namespace own
{
class A
{
};
}

in file b.cpp

namespace own
{
class B
{
};
}

Correct?

Thanks.
Correct?
Yes
Thanks very much.
Topic archived. No new replies allowed.