Some questions about namespaces

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;
	};
};
My first question is, can you declare new namespace members from different headers?
Yes. That's the whole point of namespaces, to go across files.

If the answer is yes can you access both namespace members from somewhere else if you include both headers?
Yes.

My last question is, can you access members of other namespace members?
Yes, for example. This uses namespace std in namespace MyDatabase.
1
2
3
4
5
6
7
8
9
10
#include <string>

namespace MyDatabase
{
    class Connection
    {
        std::string host;
        // ...
    };
}
Last edited on
@kbw Thanks for the answer. What about my second question ?
Hi,

Maybe you are unsure about the scope resolution operator :: ?

MyNamespace::MyClassA.a

I usually put in a namepace alias, for example when using boost:

1
2
3
namespace bu = boost::units;
namespace si = boost::units::si;


then I use those alias:

bu::quantity<si::plane_angle> ZenithAngle = 1.5 * si::radians ;

Hope that helps a bit :+)
Last edited on
Not sure what you're referring to as your second question.

You can access anything in any namespace as long as you specify the appropriate using namespace or you use the scope resolution operator ::

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// MyClassA.h
#include "MyClassB.h"
namespace MyNamespace{
	class MyClassA;
};

class MyNamespace::MyClassA{
	MyNamespace::MyClassB objB;
};

// MyClassB.h
#include "MyClassA.h"
namespace MyNamespaceB{
	class MyClassB;
};

class MyNamespace::MyClassB{
	MyNamespace::MyClassA objA;
};


It's an addition to my second question but how would I go about fixing code above ?
Last edited on
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.
Last edited on
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.
Topic archived. No new replies allowed.