My first question is, can you declare new namespace members from different headers? For example:
1 2 3 4 5 6 7 8 9
// MyClassA.h
namespace MyNamespace{
class MyClassA;
};
// MyClassB.h
namespace MyNamespace{
class MyClassB;
};
If the answer is yes can you access both namespace members from somewhere else if you include both headers? My next question is, can you declare a namespace member in one header and declare sub-members of that member in another header? For example:
1 2 3 4 5 6 7 8 9 10 11 12
// MyNamespace.h
namespace MyNamespace{
class MyClassA;
};
// MyClassA.h
#include "MyNamespace.h"
namespace MyNamespace{
class MyClassA{
int a;
};
};
My last question is, can you access members of other namespace members? For example:
1 2 3 4 5 6 7 8 9 10 11 12 13
namespace MyNamespace{
class MyClassA{
MyClassB objB;
public:
MyClassA() { objB.a = 10; objB.b = 20;};
};
class MyClassB{
private:
int a;
public:
int b;
};
};
You have recursive includes. i.e. A includes B and B includes A.
It it really necessary to have separate namespaces?
The intent of namespaces to to prevent collisions when things are named the same.
Your classes have different names, so there is no collision.
You also have no include guards in your header files.
I guess my understanding of namespaces is wrong. What I need was to have mutual classes and thought namespaces would solve that but looks like I was wrong. Upon further searching I came across a solution like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// MyClassA.h
#pragma once
class MyClassB;
class MyClassA{
MyClassB * ptrB;
public:
void usePtrB();
};
// MyClassB.h
#pragma once
class MyClassA;
class MyClassB{
MClassA * ptrA;
public:
void usePtrA();
};
I think I can still use a namespace to keep my classes organized so thank you guys for the answers.