Say I’m making a project and I have most the project in a namespace named Project. I define a class inside the namespace Project named MainProject.
In the source file, to implement the class, do I do ‘using namespace Project;’ or do I wrap it in a ‘namespace Project { … }’ nest?
Given a header “n.h”:
The following does not define
f()in namespacen(from here on, I’ll refer to it asn::f:If you try to refer to
n::fanywhere, you’ll get a link-time error. The above defines an f in the global namespace. This does definen::f:This also does:
but has a downside where if you mis-type the name or signature, you’ll add a new function to the namespace and leave
void n::f()undefined, leading to a semi-annoying link-time error.When classes are involved, things are a little different:
This will be okay, because there is no global
c:But the following will cause a link-time error if you try to add two c’s, for the same reason as with the first attempt at defining
n::f():This scenario will also cause a link-time error (or maybe even a compilation error, depending on where
::c::fis defined):