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.
Related
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.)
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;
...
}
Is it possible to add something to a namespace alias?
Or how can I achieve the following:
using KdTree = ExternalLibrary::Special::KdTree;
namespace KdTree{
class MySpezial {};
};
That does not work, but the following works:
using KdTree = ExternalLibrary::Special::KdTree;
namespace ExternalLibrary{ namespace Special { namespace KdTree{
class MySpezial {};
}}};
Is the first method just prohibited by the standart?
The first example is currently not allowed and probably won't be in C++1Z either, but note that a recent proposal is allowing
namespace ExternalLibrary::Special::KdTree {
class MySpezial {};
}
This is already implemented in Clang.
This is a rather simple question more or less considering syntax semantics.
I've got a class inside a namespace, which uses a lot of classes out of another namespace:
namespace SomeNamespace
{
class MyClass
{
//...
//These types of namespace uses occur alot around here:
void DoSomething(const anothernamespace::anotherclass &arg);
//...
}
}
This class is of course in its own .hpp file.
I would like to make everything inside the namespace "anothernamespace" available to the MyClass class, however, if I simply put it like this:
namespace SomeNamespace
{
using namespace anothernamespace;
class MyClass
{
//...
//These types of namespace uses occur alot around here:
void DoSomething(const anothernamespace::anotherclass &arg);
//...
}
}
Anyone who does
using namespace SomeNamespace;
Will automatically also use anothernamespace - which is what I want to avoid.
How do I achieve what I want?
The simplest non-perfect-but-helping solution would be to use a namespace alias :
namespace SomeNamespace
{
namespace ans = anothernamespace; // namespace alias
class MyClass
{
//...
//These types of namespace uses occur alot around here:
void DoSomething(const ans::anotherclass &arg);
//...
}
}
Your class users will not "using namespace anothernamespace;", making it more safe, but you still have to use the alias in your class. Not sure it helps, it depends on if you just want to type less or maybe hide a type. Here you put the full namespace in a kind of sub-namespace that don't get into the user's namespaces, but is still available.
Otherwise... there is no way to do exactly what you want. Using namespace don't work inside class declarations.
This does what you want. Both namespaces are accessible to MyClass. using namespace is bad practice in headers though.
namespace SomeNamespace {
namespace other {
using namespace anothernamespace;
class MyClass {
};
}}
namespace SomeNamepace {
typedef other::MyClass MyClass;
}
You really should prefer specifying anothernamespace:: in your class declaration.
You can't. It's just that simple, I'm afraid.
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;
...