I have a very basic question about namespace
when should I use "using namespace A::B"?
I know I shouldn't use it in header files, how about .cpp file? In my test.cpp:
namespace A{
namespace B{
namespace C{
A::B::Object obj = ....
}
}
}
the full namespace is A::B::Object as I have right now, Do I actually need A::B? Can I just have Object obj = ....? Should I use using namespace A::B and then have Object obj = ....? I am not sure what this actually means:
namespace A{
namespace B{
namespace C{
.....
}
}
}
I know This means all the contents inside it will have namespace A::B::C, but does it also mean:
using namespace A
using namespace A::B
using namespace A::B::C
implicitly for the contents inside it?
Because obj is not on the root (global) namespace if you write outside its namespace Object the identifier will be not found. So you have a few options:
use fully qualified name:
A::B::C::Object obj = ...
use using declaration:
using namespace A::B::C;
Object obj = ...;
use using declaration:
using A::B::C::Object
Object obj = ...
use namespace alias:
namespace X = A::B::C;
X::Object obj = ...
And basically any combination of above e.g.:
namespace Y = A::B;
Y::C Object obj = ...
using namespace A::B;
C::Object obj = ...
Statements placed in the inner namespaces will be able to reference classes, typedefs, functions, etc from the outer namespaces without explicitly typing them.
namespace A {
class AClass { };
namespace B {
class BClass {
AClass a; // Does not require A::AClass
};
}
}
In addition to using namespace, another way to shorten lengthy compounded namespaces in a .cpp file is to declare a new, shorter namespace:
// MyClass.h
namespace A {
namespace B {
class MyClass {
public:
void f();
};
}
}
// MyClass.cpp
#include "MyClass.h"
namespace AB = ::A::B;
void AB::MyClass::f() {
// impl...
}
(Note that the optional :: at the start of ::A::B explictly tells the compiler that ::A::B is the full path, which is potentially useful if a header had an SomeOtherSpace::A::B namespace and carelessly wrote using namespace SomeOtherSpace.)
Related
I have a file decl.h with the following:
namespace foo {
...
class A;
...
}
I want to use the whole of declarations from decl.h, except for class A, as I want to have another class, with the same name, declared and defined inside my def.cpp. I'm looking for something that'd allow me to do this:
# include "decl.h"
using namespace foo;
hiding foo::A;
class A {
...
};
Is there anything like that? Or the only way around is to explicitly make each desired member from foo public in my def.cpp?
Just remove using namespace foo;. That's the whole point of namespaces.
You can't hide members of a namespace, and certainly not when using a using namespace ... statement.
The whole point of namespaces is to avoid naming conflicts like you describe.
So, get rid of the using namespace foo; statement, and wrap the second class A in a different namespace, eg:
#include "decl.h"
//using namespace foo;
namespace defcpp {
class A {
...
};
}
Now def.cpp will know about foo::A and defcpp::A. You just have to qualify which one you want to use whenever you need to use an A. For example:
#include "decl.h"
//using namespace foo;
namespace defcpp {
class A {
...
};
}
class B {
defcpp::A a;
...
};
void doSomething()
{
defcpp::A a;
...
}
I have an enum in the header:
namespace somespace
{
namespace internal
{
class SomeClass
{
public:
typedef enum
{
kNone = 0,
kKaka = 1,
}SomeEnum;
}
}
}
In the cpp, we sometimes use an anonymous namespace with the helper functions.
#include <somespace/internal/SomeClass.h>
using somespace::internal;
namespace
{
bool helpMe(SomeEnum& foo) //does not recognize the enum in the header
{
}
}
void SomeClass::memberMethod
{
}
But I cannot access the SomeEnum in the .cpp file. Why is that?
How can I get around this without polluting the internal namespace for example?
SomeEnum is scoped to the class name it is declared in. To use it you need SomeClass::SomeEnum. This assumes that SomeClass is accessible in the scope you have it. If not then you need somespace::internal::SomeClass::SomeEnum
Suppose I have the following files:
// SomeClass.h
namespace Example
{
class SomeClass
{
...
SomeClass someFunction();
...
};
}
// SomeClass.cpp
Example::SomeClass Example::SomeClass::SomeFunction()
{
...
}
Would there be any consequences to add "using namespace Example;" before the namespace in SomeClass.h to eliminate the need of adding the "Example::" scope operator to things in the Someclass.cpp file? Even if there are no conesequences, would this be considered bad coding practice?
The change would be as follows:
// SomeClass.h
using namespace Example;
namespace Example
{
class SomeClass
{
...
SomeClass someFunction();
...
};
}
// SomeClass.cpp
SomeClass SomeClass::SomeFunction()
{
...
}
No, please don't put using namespace ...; in the global area. You can just do this:
SomeClass.h
// using namespace Example; // never here please
namespace Example
{
using namespace OtherExample; // this is okay (not global)
class SomeClass
{
...
SomeClass someFunction();
...
};
}
SomeClass.cpp
namespace Example // same as in .h
{
using namespace OtherExample; // this is okay (not global)
SomeClass SomeClass::SomeFunction()
{
...
}
}
And I would also suggest with potentially huge namespaces like std:: to never use using namespace std; even within your own namespaces because they simply drag in too many common symbol names.
I have a class which is not part of any namespace
class A(*) .
And I have another class with same name but part of namespace
class A part of namespace B.
In xyz.cpp, I have the below:
#include "..."
using namespace B;
// some code
A::var; // This A should be part of (*) and not namespace B.
// some code
But since I have conflicting class names, I get errors. Is there a way to get around this?
The using namespace keyword imports all of the names from the specified namespace into the global namespace. Since you already declared a class A in the global namespace, this results in a conflict.
Solution: Don't use using namespace B.
This is effectively what you're doing:
namespace GLOBAL {
class A { ... };
};
namespace B {
class A { ... };
};
using namespace B /* export 'B::A' into 'GLOBAL' resulting in a conflict; */ ;
You may not use
using namespace B;
but use like
B::A::var
instead.
I have to use an API provided by a DLL with a header like this
namespace ALongNameToType {
class ALongNameToType {
static void Foo();
}
}
Is there a way to use ALongNameToType::ALongNameToType::Foo without having to type ALongNameToType::ALongNameToType each time? I tried using using namespace ALongNameToType but got ambiguous symbol errors in Visual Studio. Changing the namespace name or removing it gives me linker errors.
I don't know what's ambiguous, but you can avoid all conflicts with other Foo functions like this:
namespace ALongNameToType {
struct ALongNameToType {
static void Foo();
};
}
typedef ALongNameToType::ALongNameToType Shortname;
int main() {
Shortname::Foo();
}
namespace myns = ALongNameToType;
It seems that you can't alias a class scope like this:
// second ALongNameToType is a class
namespace myns = ALongNameToType::ALongNameToType;
Maybe you could alias the function it self:
namespace foo
{
class foo
{
public:
static void fun()
{
}
};
}
int main()
{
void (*myfunc)() = foo::foo::fun;
myfunc();
}
using ALongNameToType::ALongNameToType::Foo;
if you just want to use it as Foo().
There are three ways to use using. One is for an entire namespace, one is for particular things in a namespace, and one is for a derived class saying it doesn't want to hide something declared/defined in a base class. You can use the second of those:
using ALongNameToType::ALongNameToType
Unfortunately this isn't working for you (due to the ambiguity of the namespace and the class having the same name). Combining this type of using with a previous answer should get rid of the ambiguity:
namespace alntt = ALongNameToType;
using alntt::ALongNameToType;
But once you've renamed the namespace, you really don't need the using statement (as long as you're comfortable writing the (shortened) namespace every time you use the class:
namespace alntt = ALongNameToType;
alntt::ALongNameToType a;
...