Namespace questions: forward declaration and mixing namespaces

I'm not very familiar with C++, and this is my first time working with more than just the namespace std. What is the difference between the following?

using MyNameSpace::MyClass;

vs

namespace MyNameSpace {class MyClass;}

After both, it seems I can now create a MyClass object. Is one way better than the other? Also, if I don't do either, can I still reference MyClass by appending MyNameSpace::MyClass before it every time I need to?

Next, if I forward declare using the second option, do I still need to #include "MyClass.h" ? In my (not very good) understanding, in C++, you make header files in order to forward declare classes so that they can be used later. So if you have already forward declared, would you still need to include the header file?

Finally, when working with multiple namespaces ( namespace Architecture and namespace Router ), I have an error I don't know how to explain. In one of my header files, I have using std::vector as the first line after my #include statements. This statement is not within any namespace, I think. When compiling, I receive the error, "'Architecture::std::vector' has not been declared". To remedy this, I can change the statement to using ::std::vector to tell it to look at global scope for std::vector .

However, I don't understand why this problem exists. Since I am declaring using std::vector at the top of my header file, why am I not already at global scope? A better question might be: how can I tell which scope I am in at the top of a header file? Additionally, I thought in C++ (and according to this answer What is the difference between writing "::namespace::identifier" and "namespace::identifier"?), if you couldn't find the name within the current scope, you would automatically look up a level until you reach global scope.

I hope these questions are well written and understandable. Hopefully I can get some answers to these questions and begin to understand how namespaces interact with one another.


using MyNameSpace::MyClass;

This using-declaration injects the name MyClass into the current scope, referring to the entity denoted by MyNameSpace::MyClass , which must have been previously declared.

namespace MyNameSpace {class MyClass;}

This is a forward declaration of the class MyClass in the namespace MyNamespace .

After both, it seems I can now create a MyClass object.

Unlikely in the second case, unless you have a using namespace MyNamespace; and the full definition of MyClass is available.

Is one way better than the other?

They do completely different things.

Also, if I don't do either, can I still reference MyClass by appending MyNameSpace::MyClass before it every time I need to?

If using MyNameSpace::MyClass; compiles (ie, MyNameSpace::MyClass has been declared), then you can do this. Otherwise, you can't.

Next, if I forward declare using the second option, do I still need to #include "MyClass.h" ? In my (not very good) understanding, in C++, you make header files in order to forward declare classes so that they can be used later. So if you have already forward declared, would you still need to include the header file?

Headers usually carry the full class definition - which includes declarations of all its data members and member functions. A forward declaration like class MyClass; will not allow you to create a MyClass object, since the compiler would not know how much memory to allocate for that object or what constructors are available.

However, I don't understand why this problem exists. Since I am declaring using std::vector at the top of my header file, why am I not already at global scope? A better question might be: how can I tell which scope I am in at the top of a header file? Additionally, I thought in C++ (and according to this answer What is the difference between writing "::namespace::identifier" and "namespace::identifier"?, if you couldn't find the name within the current scope, you would automatically look up a level until you reach global scope.

This sounds like a problem of missing braces. If a header you wrote did not close namespace Architecture , then anything you include after that header will be put into the Architecture namespace by accident. If that's not the problem, please post a MCVE.

链接地址: http://www.djcxy.com/p/29772.html

上一篇: '使用namespace foo;`没用?

下一篇: 命名空间问题:向前声明和混合命名空间